Hacker News new | past | comments | ask | show | jobs | submit login

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.



Array 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."


Yep, 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....


Right. If NodeList is compatible with Array methods, it honestly feels like it should just inherit those methods. Or implement them separately.


You can just copy them to NodeList prototype.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: