JavaScript instanceof
instanceof 运算符用于检测构造 函数 的 prototype 属性 是否出现在某个实例对象的原型链上。(MDN)
instanceof 是另一种检测类型的手段,但通常用于检测对象之 间的 关系,如某个对象是不是由某个构造 函数 生成 的。
function Person ( name ) { this . name = name ; } var person = new Person ( '小明' ) ; console . log ( person instanceof Person , ) ; // 输出 :true
1. 语法
对象 instanceof 构造 函数 ;
虽然语法是这样的,其实左侧可以是任意数据类型,但右侧必须是 一个 函数 。
否则会报如下 错误 :
[ ] instanceof { } ; // Uncaught TypeError: Right-hand side of 'instanceof' is not callable
错误 大致意思是 instanceof 的右操作数不能被 调用 。
在 JavaScript ,可被 调用 的目前只有 函数 。
2. 注意点
使用 instanceof 检测的时候,不一定只有 一个 为 true 的结果。
function Person ( name ) { this . name = name ; } var person = new Person ( '小明' ) ; console . log ( person instanceof Person , person instanceof Object , ) ; // 输出 :true
因为 instanceof 实际上是去 左操作数的原型链上寻找有没有右操作数的原型 。
person 的原型链上既匹配到 Person.prototype 又能匹配到 Object.prototype ,所以都能返回 true 。
使用的时候要注意这个问题,如判断某个对象的原型链上是否有 Object.prototype 的时候,要考虑到一些其他对象。
[ ] instanceof Object ; // true
数组的原型链上也是有 Object.prototype 的,所以做一些检测的时候要考虑一些特殊情况。
3. 小结
instanceof 可以用来检测对象和构造 函数 之 间的 关系,其检测的原理是左操作数的原型上是否有右操作数的 prototype 属性 ,所以要注意一些检测的特殊情况。
JavaScript this ? ?new 运算符与构造函数查看更多关于JavaScript instanceof的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did92442