In many cases, we all need to judge a variable by its array type. How does JavaScript determine if a variable is an array of arrays? I recently studied it and shared it with you, hoping to help.
methods of detecting objects in JavaScript
1.typeof operator
This approach is not stressful for some commonly used types, such as function, String, number, undefined, and so on, but the object that detects the array does not work.
Copy Code code as follows:
Alert (typeof null); "Object"
Alert (typeof function () {
return 1;
}); "Function"
Alert (typeof ' Dream Dragon Station '); "String"
Alert (typeof 1); "Number"
Alert (typeof a); "Undefined"
Alert (typeof undefined); "Undefined"
Alert (typeof []); "Object"
2.instanceof operator
This operator has something to do with JavaScript-oriented objects, so you need to understand the object-oriented in JavaScript first. Because this operator is the prototype object that detects whether the object's prototype chain points to the constructor.
var arr = [1,2,3,1];
Alert (arr instanceof Array); True
3. Constructor properties of Objects
In addition to instanceof, each object also has a constructor attribute, which seems to be able to make array judgments.
Copy Code code as follows:
var arr = [1,2,3,1];
Alert (Arr.constructor = = Array); True
The 2nd and 3rd methods are seemingly unassailable, but there are still holes in it, and when you're moving back and forth through multiple frame, these two approaches are Alexandria. Because each IFRAME has its own execution environment, objects that are instantiated across the frame do not share the prototype chain with each other, causing the above detection code to fail!
Copy Code code as follows:
var iframe = document.createelement (' iframe '); Create an IFRAME
Document.body.appendChild (IFRAME); Add to Body
Xarray = Window.frames[window.frames.length-1]. Array;
var arr = new Xarray (1,2,3); Declaring an array [1,2,3]
Alert (arr instanceof Array); False
Alert (Arr.constructor = = Array); False
Detecting Array type methods
These methods look impeccable, but eventually there will be some problems, and then provide you with some of the more good methods, can be said to be impeccable.
1.object.prototype.tostring
Object.prototype.toString behavior: First, get an internal property of an object [[Class]], and then, based on this property, returns a string similar to "[Object Array]" as the result (read the ECMA standard should know, [ ] used to represent an externally inaccessible property, called an internal property, used within a language. Using this method, and then with call, we can get the internal properties [[Class]] of any object, and then convert the type detection into string comparisons to achieve our goal.
Copy Code code as follows:
function Isarrayfn (o) {
return Object.prototype.toString.call (o) = = ' [Object Array] ';
}
var arr = [1,2,3,1];
Alert (ISARRAYFN (arr));/True
Call Change ToString This refers to the object to be instrumented, returns a string representation of this object, and then compares whether the string is ' [object Array] ' to determine whether it is an instance of Array. Why not direct o.tostring ()? Well, although the array inherits from object, there are toString methods, but this method may be rewritten to not achieve our requirements, And Object.prototype is the tiger's butt, few people dare to touch it, so to a certain extent to ensure its "purity":
JavaScript standard document definition: [[Class]] value may only be one of the following strings: Arguments, Array, Boolean, Date, Error, Function, JSON, Math, number, objec T, RegExp, String.
This approach is often useful when identifying built-in objects, but do not use this method for custom objects.
2.array.isarray ()
ECMASCRIPT5 will formally introduce Array.isarray () to JavaScript in order to accurately detect whether a value is an array. ie9+, Firefox 4+, Safari 5+, Opera 10.5+, and chrome all implement this approach. However, the version prior to IE8 is not supported.
3. Good reference
Combining the above methods, there is a best way to write an array of current judgments:
Copy Code code as follows:
var arr = [1,2,3,1];
var arr2 = [{abac:1, abc:2}];
function Isarrayfn (value) {
if (typeof Array.isarray = = "function") {
return Array.isarray (value);
}else{
return Object.prototype.toString.call (value) = = "[Object Array]";
}
}
Alert (ISARRAYFN (arr));/True
Alert (ISARRAYFN (ARR2));/True
How does JavaScript determine if a variable is an array of arrays? The above is what I share for you in JavaScript to judge a variable is an array of arrays of methods, hope to be helpful to everyone.