博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js中的instanceof运算符
阅读量:4364 次
发布时间:2019-06-07

本文共 2345 字,大约阅读时间需要 7 分钟。

概述

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

语法

obj instanceof Object;//true 实例obj在不在Object构造函数中

描述

instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上。


实例

1.instanceof的普通的用法,obj instanceof Object 检测Object.prototype是否存在于参数obj的原型链上。

Person的原型在p的原型链中

function Person(){};var p =new Person();console.log(p instanceof Person);//true

2.继承中判断实例是否属于它的父类

Student和Person都在s的原型链中

function Person(){};function Student(){};var p =new Person();Student.prototype=p;//继承原型var s=new Student();console.log(s instanceof Student);//trueconsole.log(s instanceof Person);//true

3.复杂用法

这里的案例要有熟练的原型链的认识才能理解

function Person() {}console.log(Object instanceof Object);     //true//第一个Object的原型链:Object=>//Object.__proto__ => Function.prototype=>Function.prototype.__proto__=>Object.prototype//第二个Object的原型:Object=> Object.prototypeconsole.log(Function instanceof Function); //true//第一个Function的原型链:Function=>Function.__proto__ => Function.prototype//第二个Function的原型:Function=>Function.prototypeconsole.log(Function instanceof Object);   //true//Function=>//Function.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype//Object => Object.prototypeconsole.log(Person instanceof Function);      //true//Person=>Person.__proto__=>Function.prototype//Function=>Function.prototypeconsole.log(String instanceof String);   //false//第一个String的原型链:String=>//String.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype//第二个String的原型链:String=>String.prototypeconsole.log(Boolean instanceof Boolean); //false//第一个Boolean的原型链:Boolean=>//Boolean.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype//第二个Boolean的原型链:Boolean=>Boolean.prototypeconsole.log(Person instanceof Person); //false//第一个Person的原型链:Person=>//Person.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype//第二个Person的原型链:Person=>Person.prototype

总结

对应上述规范做个函数模拟A instanceof B:

function _instanceof(A, B) {    var O = B.prototype;// 取B的显示原型    A = A.__proto__;// 取A的隐式原型    while (true) {        //Object.prototype.__proto__ === null        if (A === null)            return false;        if (O === A)// 这里重点:当 O 严格等于 A 时,返回 true            return true;        A = A.__proto__;    }}

  引用至:http://www.cnblogs.com/SourceKing/p/5766210.html

转载于:https://www.cnblogs.com/kefeiGame/p/10063063.html

你可能感兴趣的文章
Redis的简单动态字符串实现
查看>>
putty network error:software caused connection abort
查看>>
存储过程 <3> 和函数的区别
查看>>
高级service之ipc ADIL用法
查看>>
Django框架-基础篇
查看>>
Leetcode: Binary Tree Maximum Path Sum
查看>>
通过虚拟环境创建并开始一个django
查看>>
关于 input[type="button"] , button
查看>>
Android ViewDragHelper全然解析 自己定义ViewGroup神器
查看>>
c++ 基础 const char* 转 char*
查看>>
JS-- 小细节--你悟到了什么?
查看>>
收款 借贷
查看>>
Gson关于抽象类的序列化与反序列化
查看>>
Java面向对象之类和对象
查看>>
Oracle数据库提权(dba权限执行系统命令)
查看>>
Hydra爆破神器使用
查看>>
java.util.zip.ZipException: duplicate entry(重复依赖多版本的类库)
查看>>
Run MVC in older version of IIS
查看>>
Ajax 监听
查看>>
隐藏"站长统计"图标
查看>>