1.通过instanceof 操作符:



let arr = [];
console.log(arr instanceof Array); // true


缺点:如果网页中包含多个框架,那实际上就存在 两个以上不同的全局执行环境, 从而存在两个以上不同版本的Array构造函数。果你从一个框架向另一个框架传入一个数组,那么传入的数组与在第二个框架中原生创建的数组分别具有不同的构造函数。

2.对象构造函数的 constructor判断:



let arr = [];
console.log(arr.constructor === Array); //true


3.Array 原型链上的 isPrototypeOf(与instanceof原理类型):



let arr = [];
console.log((Array.prototype.isPrototypeOf(arr)); //true


4.Object.getPrototypeOf(),原理与instanceof类似:



let arr = [];
console.log(Object.getPrototypeOf(arr) == Array.prototype); // true


5.Object.prototype.toString(),虽然Array也继承自Object,但js在Array.prototype上重写了toString,而我们通过toString.call(arr)实际上是通过原型链调用了。



let arr = [];
console.log(Object.prototype.toString.call(arr) == '[object Array]'); //true


6.通过Array.isArray()



let arr = [];
console.log(Array.isArray(arr)); // true