Instanceof运算符

31 阅读1分钟

定义:Instanceof 运算符用来判断一个构造函数的prototype属性所指向的对象,是否存在另一个要检测对象的原型链上

举例

  1. Function instanceof Object
  • 结果:true
  • 原因:Function.proto.proto == Object.prototype.
  1. Object instanceof Function
  • 结果:true
  • 原因:Object.proto = Function.prototype
  1. {} instanceof Object
  • 结果:true
  1. [] instanceof Array
  • 结果:true
  1. [] instanceof Object
  • 结果:true
  1. "123" instanceof String
  • 结果:false
  1. new String(123) instanceof String
  • 结果:true
  1. f instanceof Function (function f(){})
  • 结果:true
  1. f instanceof Object (function f(){})
  • 结果:true