Re:I will pay you cash to delete your NPM module
by 19h on Tuesday November 16, 2021 @10:02PM
(#29248909)
It’s also wrong because the representation wasn’t exactly always deterministic in 2013 across all js engines. The right way to check for an array is value.constructor === Array, which also doesn’t really make any sense because an array in JS really is just an object with a length key and respective keys names 0…n.
[ Parent
| on HN ]
Re:I will pay you cash to delete your NPM module
by l0b0 on Tuesday November 16, 2021 @11:52PM
(#29249406)
I've been using JS on and off in various incarnations, and this sort of blows my mind. First because I don't think I've ever seen that approach used anywhere, and second because it would probably take me upwards of an hour (possibly a day) to work out whether that would actually work.
Re:I will pay you cash to delete your NPM module
by zirak on Wednesday November 17, 2021 @02:50PM
(#29258022)
The constructor check would unfortunately not work for cross-frame arrays. Each frame has its own global object, with its own Array constructor. So:
var iframe = document.createElement('frame');
document.body.appendChild(iframe);
var value = iframe.contentWindow.Array();
value.constructor === Array // false
value instanceof Array // false
value instanceof iframe.contentWindow.Array // true
Stringifying the constructor works even for cross-frame values: value.constructor.toString() === Array.toString() // true
These days though, `Array.isArray` is the right thing to do, available since roughly 2010.