1. instanceof操作符
var arr = [1,2,3]
console.log(arr instanceof Array) //true
2. 对象的constructor属性
var arr = [1,2,3]
console.log(arr.constructor === Array) //true
3. Array.isArray()(ie9以上支持)
Array.isArray([1, 2, 3]); // true
4. Object.prototype.toString
这个方法稍微绕了点弯,我们先看以下一下例子就明白了。
var num = 1
var str = 'abc'
console.log(Object.prototype.toString.()) //[object Object]
console.log(Object.prototype.toString.call(str))//[object String]
console.log(Object.prototype.toString.call(num))//[object Number]
call改变toString的this引用为待检测的对象,返回此对象的字符串表示,然后对比此字符串是否是’[object Array]’,以判断其是否是Array的实例。
var arr= [1,1]
console.log(Object.prototype.toString.call(arr))