1, a real array of judging methods
The simplest method of declaring an array in JavaScript is:
var a = [];
The most straightforward way to determine whether an array is:
Copy CodeThe code is as follows:
A instanceof Array//true
A. Constructor = = Array//true
Here is a instanceof syntax, instanceof is a cloud operator, as with "+-*/", its syntax is as follows:
result = obj Intanceof class
is used to determine whether an object is an instance of a class, and the result of the operation returns TRUE or FALSE. The definition of class in JavaScript is also initialized by constructors, so the right operator class of the instanceof syntax must be an instance of function, that is, the class instanceof function must be true, And if the right operator is not a function when using instanceof, the TypeError exception is thrown. All objects are instances of object, so any object instanceof objects returns True. While we say that objects are initialized by constructors, instanceof does not by checking whether an object is constructed by the function, but by whether it is inherited by the constructor's prototype, the following example illustrates the problem:
Copy CodeThe code is as follows:
function Range (Low, high) {
This.low = low;
This.high = high;
}
Range.prototype.constructor = = Range; True
Range.prototype = {
Include:function (x) {return (x >= this.low && x <= this.high);},
Exclude:function (x) {return (x < this.low && x > This.high);}
}
var r = new Range (0, 100);
R instanceof Range; False
R instanceof Object; True
Range.prototype.constructor = = Objecct; True
Although R is constructed from the new range, R is not an instance of range, which is the problem, The Range.prototype assignment statement overrides the default constructor and does not Range.prototype.constructor to a range before assigning a value to prototype, and then it becomes an object, which is understood because
Copy CodeThe code is as follows:
Range.prototype = {
Include:function (x) {return (x >= this.low && x <= this.high);},
Exclude:function (x) {return (x < this.low && x > This.high);}
}
is actually equivalent to:
Copy CodeThe code is as follows:
Range.prototype = new Object ({
Include:function (x) {return (x >= this.low && x <= this.high);},
Exclude:function (x) {return (x < this.low && x > This.high);}
});
So Range.prototype.constructor = = Object, then the instance created by the new range is, of course, an instance of object.
Look at the official explanation more directly:
The instanceof operator does not actually check whether R is initialized by the Range constructor. It checks whether it inherits from Range.prototype.
JavaScript also has a function typeof similar to instanceof, but it returns the specific basic data type: Number,string,function,object,undefined,boolean, Only these six, not within these six ranges, return object, which means that typeof ([]) returns an object instead of an array.
Another syntax involved is the constructor of the Constructor,constructor return object:
Copy CodeThe code is as follows:
var a = [];
A.constructor; Array
constructor is an object initialization function, with new call, if the object is an array, then its constructor should be an array, its own class is not necessarily, because it may be prototype in the constructor changes.
2, the method of judging pseudo-array
There is a pseudo-array in JavaScript that can be traversed using an array-like traversal method, with the length property getting the element's lengths, and using the [] subscript to get the specified element, which we call a pseudo-array, and the object in jquery is a typical pseudo-array, such as:
So judging whether it is the key to the pseudo-array is to determine whether there is a length property, whether there is a basic array operation function Splice, the following is the judgment method:
Copy CodeThe code is as follows:
var Is_array = function (value) {
return value &&
typeof value = = = ' object ' &&
typeof Value.length = = = ' Number ' &&
typeof Value.splice = = = ' function ' &&
! (value.propertyisenumerable (' length '));
};
Here propertyisenumerable is used to determine whether the length property can be enumerated, in fact, the original string object also has an array-like effect, but we can not use it as an array object, so we need to judge typeof value = = " Object ", because TypeOf is a string object that returns a string.
From: http://www.jb51.net/article/50056.htm
JS to determine whether an object is an array