JavaScript method to determine whether the variable is an Array (Array), criptarray
Today, I want to give you some knowledge about javascript to determine whether a variable is an Array (Array). I will talk about it through the following four points:
1. Is typeof really so powerful ??
// First check the code var ary = [, 4]; console. log (typeof ary); // The output result is the Object
The above method does not detect whether it is an array in real time, but only determines its type. So typeof is quite good in determining the basic type of data, however, it is not possible to accurately test whether it is an array (the specific usage of typeof will be mentioned later, and now the regression question)
2. instanceof judgment
var ary = [1,23,4];console.log(ary instanceof Array)//true;
From the output results, it is quite satisfactory. We can accurately check whether the data type is an array. Don't be happy too early. Let's first think about this shortcoming, let's talk about the third method.
3. Prototype Chain Method
Var ary = [1, 23, 4]; console. log (ary. _ proto __. constructor = Array); // trueconsole. log (ary. constructor = Array) // true the two pieces of code are the same
This method is very advanced ~~, The prototype chain method is used. However, this method is compatible. in earlier versions of IE, _ proto _ is not defined ~ In addition, this still has limitations. Let's summarize the limitations of the 2nd methods and the 3rd methods.
Summarize the limitations of the 2nd and 3rd methods.
Variables determined by instanceof and constructor must be declared on the current page. For example, a page (parent page) has a framework that references a page (Child page ), on the child page, declare a ary and assign it to a variable on the parent page. Then, judge the variable, Array = object. constructor; returns false;
Cause:
1. array is a type of referenced data. In the transfer process, only the reference address is transmitted.
2. The address referenced by the Array native object on each page is different. The constructor corresponding to the array declared on the subpage is the Array object of the subpage; the parent page is used for judgment. The Array used is not equal to the Array of the Child page. Remember, otherwise it is difficult to track the problem!
4. General methods
var ary = [1,23,4];function isArray(o){return Object.prototype.toString.call(o)=='[object Array]';}console.log(isArray(ary));
For more information about Object. prototype. toString usage, see Object. prototype. toString usage.
Well, the method (Array) for JavaScript to determine whether a variable is an Array is described so much. Today we mainly summarize these four methods, if this article is not well written, please kindly advise. Thank you!
Articles you may be interested in:
- Multiple variable definitions in js (direct object volume, direct array volume, and direct function volume)
- Javascript checks whether a variable is an array or an object (array or object)
- JavaScript method for determining whether a variable is an object or an array
- Methods In JavaScript to determine whether a variable is an array, function, or object type