JS判断一个元素是否是数组的方法

方法一:通过instanceof判断

 instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。

function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);

console.log(auto instanceof Car);
// expected output: true

console.log(auto instanceof Object);
// expected output: true

方法二:通过constructor判断

 方法三: 通过Object.prototype.toString.call()判断

1 let a = [1,2,3]
2 Object.prototype.toString.call(a) === '[object Array]';//true

方法四:通过Array.isArray()判断

原文地址:https://www.cnblogs.com/zhizhi0810/p/14685636.html