January 2020
Intermediate to advanced
548 pages
13h 36m
English
Most native Array methods are generic, meaning that they can be used on any object that looks like an array. All we need to achieve the appearance of an array is use a length property and individual properties for each index (indexed from zero):
const arrayLikeThing = { length: 3, 0: 'Suspiciously', 1: 'similar to', 2: 'an array...'};// We can "borrow" an array's join method by assigning // it to our object:arrayLikeThing.join = [].join;arrayLikeThing.join(' ');// => "Suspiciously similar to an array..."
Here, we've constructed an array-like object and then provided it with a join method of its own by borrowing the join method of an array (that is, from Array.prototype). The native array join method is so generically implemented ...
Read now
Unlock full access