ZSWYD

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 是一个一元运算符在一个运算数之前,运算数可以是任意类型。它返回值是一个字符串,该字符串说明运算数的类型。

posted on 2022-08-06 11:15  苏舒  阅读(31)  评论(0)    收藏  举报

导航