How to detect whether a variable is an array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.