1.用typeof运算符来判断
2.用instanceof判断
instanceof运算符可以用来判断某个构造函数的prototype属性所指向的對象是否存在于另外一个要检测对象的原型链上。
在使用的时候语法如下: object instanceof constructor
3.用constructor判断 console.log(a.constructor == Array);//true
4.用Object的toString方法判断 const a = ['Hello','Howard'];
Object.prototype.toString.call(a);
//"[object Array]" Object.prototype.toString.apply(a);
//"[object Array]"
总结:
const isArray = (something)=>{ return Object.prototype.toString.call(something) === '[object Array]'; }
cosnt a = [];
const b = {}; isArray(a);//true isArray(b);//false
5.用Array对象的isArray方法判断
const a = []; const b = {}; Array.isArray(a);//true Array.isArray(b);//false