废话不多刷!直接来正题吧!
1.使用数组自带的方法:Array.isArray() 常用方法
let arr=[1,2,3]
let obj={}
console.log(Array.isArray(arr)) //true
console.log(Array.isArray(obj)) //false
2.使用constructor(constructor是一个构造器,指明了是由什么构造出来的) 其实 let arr=[] 可以这样写
let arr= new Array()
所以arr是由Array函数创建出来的
console.log(arr) //有一个constructor:f Array()
这是一个obj对象
let arr=[1,2,3]
let obj={}
console.log(arr.constructor===Array) //true
console.log(obj.constructor==Array) //false
3.使用instanceof()instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链
let arr=[1,2,3]
let obj={}
console.log(arr instanceof Array) //true
console.log(obj instanceof Array) //false
4.使用isprototypeof()(该方法用于判断一个对象是否存在一个对象的原型链上)
let arr = [];//上面提过相当于let arr =new Array()
console.log(Array.prototype.isPrototypeOf(arr)); // true
5.跨原型链调用tostring()
每一个继承 Object 的对象都有 toString方法,如果 toString 方法没有重写的话,会返回 [Object type],其中 type 为对象的类型。但当变量类型不为对象时,使用 toString 方法会直接返回数据内容的字符串,所以我们需要使用call或者apply方法来改变toString方法的执行上下文
不改变作用域的时候:
使用call
const an = ['Hello','An'];
an.toString(); // "Hello,An"
Object.prototype.toString.call(an); // "[object Array]"