js 判断值为Array or Object的方法

时间:2023-12-10 17:06:08

Array.prototype.isPrototypeOf(obj)

Object.prototype.toString.call(obj)

Array.isArray(obj)

实例:

//typeof()  【原始类型:可分辨;引用类型:object】
console.log(typeof([])); //object
console.log(typeof({})); //object
//Array.prototype.isPrototypeof(obj)
console.log(Array.prototype.isPrototypeOf([])); //true
console.log(Array.prototype.isPrototypeOf({})); //false //Object.prototype.toString.call(obj)
console.log(Object.prototype.toString.call([])); //[object Array]
console.log(Object.prototype.toString.call({})); //[object Object] //ES5: Array.isArray(obj) 【兼容性】
console.log(Array.isArray([])); //true
console.log(Array.isArray({})); //false