typeof、instanceof、constructor...

let type = []
type.constructor = "hello"
复制代码

typeof

【不能进一步区分null、数组、对象...】

console.log(typeof type) // object
复制代码

instanceof

【基本类型需用new声明,且还受继承影响】

console.log(type instanceof Array, type instanceof Object) // true true
复制代码

constructor

【指向能够改变】

console.log(type.constructor) // hello
复制代码

通用:Object.prototype.toString.call( )

console.log(Object.prototype.toString.call(type)) // [object Array]
console.log(Object.prototype.toString.call(type).replace(/\[object\s|\]/g, '')) // Array
复制代码
相关文章
相关标签/搜索