JavaScript怎么判断一个数组是数组呢?

2020-03-26  本文已影响0人  六寸光阴丶

写在前面

如果本文对您有所帮助,就请点个关注吧!

解决方案

方法一:instanceof

variable instanceof Array

方法二:原型prototype + toString + call

Object.prototype.toString.call(variable).indexOf('Array') !== -1;

方法三:原型prototype + isPrototypeOf()方法

Array.prototype.isPrototypeOf(variable) 

方法四:构造函数 constructor

variable.constructor.toString().indexOf("Array") !== -1

方法五:数组方法 isArray()

Array.isArray(variable);
上一篇 下一篇

猜你喜欢

热点阅读