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] }
判断数组是数组,对象是对象的方法
最新推荐文章于 2023-04-23 00:21:00 发布