js判断数组是数组,对象是对象的几种方式

55 阅读1分钟

我正在参加「码上掘金挑战赛」详情请看:码上掘金挑战赛来了!

let arr = [1,2,3];
let obj = {a:1};
let a = 1
//instanceof 只能鉴别引用数据类型   typeof只能鉴别基本数据类型
console.log(Object.prototype.toString(arr),Object.prototype.toString(obj))
//Object.prototype.toString.call()在IE6一下 null和undefined均为Object
console.log(Object.prototype.toString.call(arr),Object.prototype.toString.call(obj),Object.prototype.toString.call(null),Object.prototype.toString.call(undefined))
//[object Array] [object Object]  [object Null] [object Undefined]
console.log(arr instanceof Array,obj instanceof Array,typeof arr,typeof obj)//true  false 'object' 'object'
console.log(a instanceof Number,typeof a,null instanceof Object,undefined instanceof Object)//false 'number' false  false
console.log(Array.isArray(arr)) //true
console.log(arr.constructor,obj.constructor,a.constructor)
//ƒ Array() { [native code] }   ƒ Object() { [native code] }  ƒ Number() { [native code] }