首页 > 其他 > 详细

instanceof判断问题

时间:2020-03-27 13:47:14      阅读:74      评论:0      收藏:0      [点我收藏+]

有时候我们根据instanceof来判断对象的数据类型

但是 用instanceof判断基本数据类型时 会不靠谱

例如

let str = ‘123‘

let str1 = new String("123")

console.log(str instanceof String)  //false

console.log(str1 instanceof String ) //true

这时候,可以重写instanceof的判断规则

class typeofString{

  static [Symbol.hasInstance](x){

    return typeof str === "string"    

  }

}

console.log(str typeof typeofString)  //true

instanceof判断问题

原文:https://www.cnblogs.com/vnwith/p/12580524.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!