4 ms·
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.const
by 19h 5y ago
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.
- l0b0 5y agoI'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.
- zirak 5y agoThe 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.