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   苏舒  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示