JavaScript construct called destructuring

Keesha Hargrove
3 min readJun 13, 2022

A JavaScript construct called destructuring can be used to extract values from nested arrays or properties from objects, and can also be used with nested object, parameters and nested arrays. A nested array is a list of one or more arrays, each item in the outer array is the name of an array and each item in that list is the name of a property in the object corresponding to that array.

JavaScript can be used to add and remove properties of an object:

var person = { firstName : ‘John’ , lastName : ‘Doe’ }; person . address = { street : ‘1 Main Road’ , city : ‘New York City’ };

Mixin enables inheritance by allowing an object to include properties and methods from other objects. For example, take two classes named Person and Company. A Person can be “mixed into” a Company, which means that Company will inherit all instance variables (variables defined using var ) and methods (defined either using function statements or with the prototype property) from Person. Like this:

Company = Person . extend ({ initialize : function (){ //… }, … }); var company = new Company (); company . firstName = ‘Ivan’ ; company . lastName = ‘Nakhimovskij’ ; company . getSSN () //returns 123–45–6789. console . log ( company ); //logs: Ivan Nakhimovskij Company “extend”-ed from Person {firstName:’Ivan’, lastName:’Nakhimovskij’, getSSN(): 123–45–6789}.

There may be times when you want to get a particular instance of a function from an object by calling some method on the object. The tricky part is that the “some-method” might create the instance of a desired class, and then return it for further program usage. What do you do in that case, get both object and its instance? Here are ways to code with this kind of situation:

//#1: Easy to read, but slow to execute Person . instances . getByName . apply ( {} , person . instances ); javascripts.coffee

In JavaScript the getByName method returns an instance from an object. In this example, it simply searched all objects for a desired instance of a person with the given name. That’s easy to use, but very slow at run-time since JavaScript still has do create all objects when a program runs no matter if they eventually will or will not be used. The reason why this code is so slow is that it finds all instances of every possible kind you could have created and then loops through them looking for the right one.

//#2: Fast, runs very fast in theory initially, finally becomes slow javascripts.coffee

A much faster solution uses recursion: Put the unique conditions (or their results) in “if” conditions at the beginning of the function that needs needed function(s): Make things simpler, not more complex by deleting unnecessary logging and smooth handling of other agencies outside our desired goal… in this case collect instances associated himself first with a person and throw away everything else immediately after that; this blocks other threads from making unnecessary work.. next create outer loops for any number of remaining objects instead of one big one for all entities existing out there!

--

--