- let a={
- a:1
- }
- let b=[2,3]
- console.log(a.toString())
- console.log(b.toString())
- //[object Object],输出了a的数据类型
- //2,3 把数组内的值转化为了字符串
-
js其实是重写了某些数据类型的toString方法,所以才会造上面的情况,array有其对应的toString方法,就近调用了
let a={
a:1
}
let b=[2,3]
delete Array.prototype.toString
console.log(a.toString())
console.log(b.toString())
这样子就会输出 [object Object] [object Array]
优雅点的方法就是,让你要判断的变量的数据去调用Object.prototype.toString的方法,这样就可以去准确的判断变量类型