Whether or not to spread an object is determined via the spec operation IsConcatSpreadable().
The reason that that works is because constructors inherit from each other in ECMAScript 6 (a super-constructor is the prototype of its sub-constructors). For example, if you create a subclass MyArray of Array and want to convert such an object to an instance of MyArray, you simply use om(). 18.2.1.2 from() in subclasses of Array #Īnother use case for om() is to convert an Array-like or iterable value to an instance of a subclass of Array. Here, we skipped that intermediate step via a generic method call and via the two-parameter version of om(). Previously, we converted the Array-like object to an Array in order to call forEach().
In this example, the result of document.querySelectorAll() is again an Array-like object, not an Array, which is why we couldn’t invoke map() on it. textContent ) // om(): const names2 = Array. querySelectorAll ( 'span.name' ) // map(), generically: const names1 = Array. The following is an example of converting an Array-like object to an Array:Ĭonst spans = document. Strings and Arrays are iterable, as are ECMAScript’s new data structures Map and Set.
That have been deleted or are uninitialized. ForEach() calls a provided callbackFn function onceįor each element in an array in ascending index order.