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
複製代碼
相關文章
相關標籤/搜索