Crockford poses a solution in Javascript: The Good Parts for identifying if an object is an array which goes something like this:
var isArray = function (obj) {
return obj && typeof obj === "object" && obj.constructor === Array;
}
But then he goes on to state that
…it fails to identify arrays that were constructed in a different window or frame…
Why doesn’t this solution work in that situation?
The symbol “Array” is something that’s local to each window. Checking to see if an object constructed in a different window has a particular native constructor therefore doesn’t work.
That is: in window #1, there’s an “Array” constructor function. There’s also one in window #2. They’re the same, of course, but they’re different because they’re distinct objects. When the comparison is made, it’s made the way any object comparison is made: either the two values are references to exactly the same object, or they’re not.
It’s kind-of odd that JavaScript works this way, but it’s the nature of the language.