单纯记录几个方法
1.typeof
使用typeof需要注意的是,在判断数组和null时都会判读为object对象;
使用方法就是直接 :typeof 需要判断的数据。
2.instanceof
instanceof是监测对象的原型链是否指向构造函数的prototype对象;
使用方法 :arr instancef Array,判断arr是不是数组。
3.查看constructor属性
对象有原型链,所以看他的constructor属性也是可以判断是不是数组
使用方法:console.log(arr.constructor === Array)
4.arr.isArray()
5.Object.prototype.toString
通过检测其属性判断
使用方法
function isArrayFn (o) {
return Object.prototype.toString.call(o) === '[object Array]';
}
var arr = [1,2,3,1];
alert(isArrayFn(arr));// true