Does JS judge whether an object is an array object?

Determine whether an object is an array in object-oriented judgment

1 You can find whether its prototype (__proto__) is the prototype object of the array object

 var arr1 = [1,2,3];
 console.log(arr1.__proto__==Array.prototype);     //true

2 instanceof

var arr1 = [1,2,3];
console.log( arr1 instanceof Array);     // true;

3 Array.isArray()

Array method 

var arr1 = [1,2,3];
console.log(Array.isArray(arr1);      //true;

4 Object.prototype.toString.call()

var  arr1 = [1,2,3];
console.log(Object.prototype.toString.call(arr1);  //'[object Array]'

5 Constructor name

var arr = [1,2,3];
console.log(arr.constructor.name);    //Array

Guess you like

Origin blog.csdn.net/weixin_46034375/article/details/108697683