js判断一个对象是不是数组
1. 原型 Array.prototype.isPrototypeOf(obj);
- Array.prototype.isPrototypeOf([]) // true
- Array.prototype.isPrototypeOf({}) // false
2. 构造函数
2.1 instanceof Array
- [] instanceof Array // true
- {} instanceof Array // false
2.2 obj.constructor === Array
- [].constructor === Array //true
- {}.constructor === Array //false
3.类属性 Object.prototype.toString.call()
- Object.prototype.toString.call([]) //[object Array]
- Object.prototype.toString.call({}) //[object Object]
- Object.prototype.toString.call(null) // [object Null]
4.Array.isArray
- Array.isArray([]) // true
- Array.isArray({}) // false
- Array.isArray(null) // false