5 ms·
You should forget about that too. It's applying a function from one object to something else that it wasn't designed for. It happens to work in this case becaus
by guntars 5y ago
You should forget about that too. It's applying a function from one object to something else that it wasn't designed for. It happens to work in this case because $nodeList and Arrays are both array-like, but that's a lucky happenstance.
- bluepnume 5y agoRight. If NodeList is compatible with Array methods, it honestly feels like it should just inherit those methods. Or implement them separately.
- megous 5y agoYou can just copy them to NodeList prototype.
- sgentle 5y agoArray prototype methods are designed to be applied to other objects. There's explicit language about it dating back to the first edition of the ECMAScript spec: "The map function is intentionally generic; it does not require that its this value be an Array object. Therefore it can be transferred to other kinds of objects for use as a method."
- guntars 5y agoYep, I just looked it up and it does indeed say that, at least for the 2016 version when all of these features were introduced. Fine! Carry on applying your maps to your non-Arrays! Personally I think Array.of($nodeList).map(…) should be guaranteed to allocate once and then there’d be no more discussion here. https://262.ecma-international.org/7.0/#sec-array.prototype.map https://262.ecma-international.org/7.0/#sec-array.prototype....