欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

instance of

最编程 2024-03-16 18:09:28
...

instance of 运算符用于检验构造函数的 prototype 属性是否出现在某个实例对象的原型链上 

使用方法:

object instanceof constructor

object 为实例对象,constructor 为构造函数 

构造函数通过 new 可以创建一个实例对象instanceof 能判断这个实例对象是否是之前那个构造函数生成的对象

// 定义构建函数
let Car = function() {}
// 创建实例对象
let benz = new Car()
benz instanceof Car // true
let car = new String('xxx')
car instanceof String // true
let str = 'xxx'
str instanceof String // false

顺着原型链去找,直到找到相同的原型对象,返回true,否则为false