I’m curious about whether there is any way to fake out Array.isArray() with a user-defined object.
From the book JavaScript Patterns:
Array.isArray([]); // true
// trying to fool the check
// with an array-like object
Array.isArray({
length: 1,
"0": 1,
slice: function () {}
}); // false
That object clearly fails, but is there any other way to do it? This is sheer curiosity, and not because I think that you could ever screw with .isArray() in regular client code (though it would obviously be fantastic to know if you could!).
Only if you set the internal
[[Class]]property to"Array", which is not possible afaik. From the specification:Or you go the other way round: Create a normal array and explicitly set every array method to
undefined.