instanceof, typeof, object.prototype.toString.call()
instanceof, typeof, object.prototype.toString.call()
instanceof 用来判断某个构造函数的prototype属性所指向的对象是否存在于另外一个要检测对象的原型链上
点击查看代码
function instanceOf(target,type) {
return target instanceof type
}
console.log(instanceOf([],Array)) // true
console.log(instanceOf([],Object)) // true
console.log(instanceOf('1',String)) // false
so Array与Object被instanceof Object都会返回true
Object.prototype.toString.call()
如果 toString 方法没有重写的话,会返回 [Object type],其中 type 为对象的类型
该方法可以判断所有的基本数据类型
function type(target) {
return Object.prototype.toString.call(target)
}
console.log("type",type([1,2,3])) // [object Array]
console.log("type",type("string")) // [object String]
console.log("type",type(null)) // [object Null]
console.log("type",type(function(){})) // [object Function]
typeof
typeof 是一个一元运算符在一个运算数之前,运算数可以是任意类型。它返回值是一个字符串,该字符串说明运算数的类型。
没有什么是一蹴而就的。
浙公网安备 33010602011771号