In ECMAScript3, there is only one global scope in a Web page, and using the instanceof operator can
This is our conventional idea.
if (value instanceof Array)
{
Working with arrays
}
However, when a Web page contains multiple frames (one page contains more than one frame), there are actually several different global execution environments, and there are several different versions of the array, and when passing in arrays from one frame to another, there are different constructors for the incoming array and the current array
(maybe some people still don't understand, then I'll explain it again, array is a property of the global window, multiple frames have multiple windows)
So in ECMAScript5 a method is added IsArray (), regardless of the global environment in which the array is created, he just determines whether it is an array. I once saw a question that was to examine the problem.
if (Array.isarray (Vlaue))
{
Working with arrays
}
Of course there are many compatibility issues with ECMASCRIPT5, and browsers that support the Array.isarray () method have ie9+, Firefox 4+, Safari 5+, Opera 10.5+, and Chrome.
So the question is, how to accurately detect an array in a browser that has not yet implemented this method
Because the constructor name of the native array is independent of the global scope, it is guaranteed to return the same result with the ToString () method
function IsArray (value) {
return Object.prototype.toString.call (value) ==[object Array];
};
Such a method can be a perfect solution to determine whether the number of arrays, the same can be judged is not a function
function Isfunction (value) {
return Object.prototype.toString.call (value) ==[object Function];
};
Is it a regular expression?
function Isregexp (value) {
return Object.prototype.toString.call (value) ==[object REGEXP];
};
Although this seems to solve the problem, there is actually a problem: Object.prototpye.toString () itself may be modified, this is not discussed here, understand the above is enough
How to detect whether a variable is an array