js中判断数组的方式有哪些?


1.通过Object.prototype.toString.call来判断

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

2.通过instanceof来判断

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

3.通过constructor来判断

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

4.通过原型链来判断

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

5.通过ES6.Array.isAaary()来判断

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

6.通过Array.prototype.isPrototypeOf来判断

console.log(Array.prototype.isPrototypeOf([1, 2, 3]));//true

猜你喜欢

转载自blog.csdn.net/zhaojiaxing123/article/details/129256545