Js syntax to determine whether an object is an array

  • 2020-03-30 02:58:33
  • OfStack

1. Real array judgment method

The simplest method to declare an array in javascript is:
Var a = [];
The most direct way to determine whether it is an array is:
 
a instanceof Array //true 
a .constructor == Array //true 

Here, an instanceof syntax is involved. Instanceof is a cloud operator. Like "+-*/", 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 returns true or false. The definition of class in javascript is initialized by the constructor, so the right operator class of instanceof syntax must be an instanceof Function, that is, class instanceof Function must be true, and if the right operator of instanceof is not Function, a TypeError exception will be thrown. All objects are instances of Object, so any Object instanceof Object returns true. Although we say that objects are initialized by constructors, instanceof does not check whether objects are constructed by this function, but whether they are inherited by the constructor's prototype. The following example illustrates this problem:
 
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 

Here while r is through new Range structure, but it is not the Range r as an example, this is the problem, the Range, the prototype assignment statement covering the default constructor, Range before and assign the prototype. The prototype. The constructor for the Range, the assignment into Object, after the understanding, because
 
Range.prototype = { 
include: function(x){ return (x >= this.low && x <= this.high); }, 
exclude: function(x){ return (x < this.low && x > this.high); } 
} 

It is equivalent to:
 
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 the Range. The prototype. The constructor = = Object, then through the instance of the new Range created, of course, is an instance of the Object.
The official explanation is more straightforward:
The instanceof operator does not actually check whether r was initialized by The Range constructor. It checks whether It inherits from Range. Prototype.
There is also a function typeof in javascript that has the same function as instanceof, but it returns the specific basic data types: number, string, function, object, undefined, and Boolean. There are only six types, and all of them return object, that is, typeof([]) returns object instead of array.
Another syntax involved is constructor, which returns the constructor of an object:
 
var a = []; 
a.constructor; //Array 

If the object is an Array, then the constructor should be an Array. If the object is an Array, then the constructor should be an Array.

2. Pseudo-array judgment method

In javascript, there is a pseudo-array, which can be traversed by the traversal method similar to Array. The length attribute can be used to get the length of the element, and the [] subscript can be used to get the specified element. This object is called a pseudo-array.
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201405/20140513093714.png? 201441393746 ">  
Therefore, the key to determining whether an array is pseudo-is to determine whether there is a length attribute and whether there is a basic array operation function splice. Here is how to determine:
 
var is_array = function(value) { 
return value && 
typeof value === 'object' && 
typeof value.length === 'number' && 
typeof value.splice === 'function' && 
!(value.propertyIsEnumerable('length')); 
}; 

Here the propertyIsEnumerable property is used to determine whether the length propertyIsEnumerable or not. In fact, the native String object also has the effect of Array, but we can't treat it as an Array object, so we need to determine typeof value == "object", because typeof a String object returns a String.

Related articles: