-
Notifications
You must be signed in to change notification settings - Fork 0
Description
instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const auto = new Car('Honda', 'Accord', 1998);
console.log(auto instanceof Car);
// true
console.log(auto instanceof Object);
// true
A构造函数 -> 实例化 -> 实例a
实例a -> __proto__
-> A.prototype -> __proto__
-> Object.prototype -> __proto__
-> null
Function构造函数 -> 原型 -> Function.prototype -> __proto__
-> Object.prototype -> __proto__
-> null
A构造函数 -> __proto__
-> Function.prototype -> __proto__
-> Object.prototype -> __proto__
-> null
Object构造函数 -> 原型 -> Object.prototype -> __proto__
-> null
Object构造函数 -> __proto
-> Function.prototype -> __proto__
-> Object.prototype->
proto` -> null
Function构造函数 -> 实例化 -> A构造函数 -> 实例化 -> 实例a
Function构造函数 -> 实例化 -> Object构造函数 -> __proto__
-> Function.prototype -> __proto__
-> Object.prototype -> __proto__
-> null
语法
object instanceof constructor
: object 某个实例对象 constructor 某个构造函数
instanceof 运算符用来检测 constructor.prototype 是否存在于参数 object 的原型链上
// 定义构造函数
function C(){}
function D(){}
var o = new C();
o instanceof C; // true,因为 Object.getPrototypeOf(o) === C.prototype
o instanceof D; // false,因为 D.prototype 不在 o 的原型链上
o instanceof Object; // true,因为 Object.prototype.isPrototypeOf(o) 返回 true
C.prototype instanceof Object // true,同上
C.prototype = {};
var o2 = new C();
o2 instanceof C; // true
o instanceof C; // false,C.prototype 指向了一个空对象,这个空对象不在 o 的原型链上.
D.prototype = new C(); // 继承
var o3 = new D();
o3 instanceof D; // true
o3 instanceof C; // true 因为 C.prototype 现在在 o3 的原型链上
var simpleStr = "This is a simple string";
var myString = new String();
var newStr = new String("String created with constructor");
var myDate = new Date();
var myObj = {};
var myNonObj = Object.create(null);
simpleStr instanceof String; // 返回 false, 非对象实例,因此返回 false
myString instanceof String; // 返回 true
newStr instanceof String; // 返回 true
myString instanceof Object; // 返回 true
myObj instanceof Object; // 返回 true, 尽管原型没有定义
({}) instanceof Object; // 返回 true, 同上
myNonObj instanceof Object; // 返回 false, 一种创建非 Object 实例的对象的方法
myString instanceof Date; //返回 false
myDate instanceof Date; // 返回 true
myDate instanceof Object; // 返回 true
myDate instanceof String; // 返回 false
要检测对象不是某个构造函数的实例时,你可以这样做
if (!(mycar instanceof Car)) {
// Do something, like mycar = new Car(mycar)
}
function instanceof (L, R) {
// L实例对象
// R构造函数
// 取R的显式原型
let O = R.prototype;
// 取L的隐式原型
L = L.__proto__;
while(true) {
// 循环遍历
if(L===Null) {
return false;
}
if(O === L) {
return true;
}
L = L.__proto__;
}
}
function instanceof(left,right){
// 获取类型的原型
let prototype = right.prototype
// 获取对象的原型
left = left._proto_
//判断对象的类型是否等于类型的原型
while(true){
if(left === null)
return false
if(prototype === left)
return true
left = left._proto_
}
}
function myInstanceof(left, right) {
let proto = Object.getPrototypeOf(left); // 获取对象的原型
let prototype = right.prototype; // 获取构造函数的 prototype 对象
// 判断构造函数的 prototype 对象是否在对象的原型链上
while (true) {
if (!proto) return false;
if (proto === prototype) return true;
proto = Object.getPrototypeOf(proto);
}
}
function InstanceOf({ __proto__ }, { prototype }) {
while (true) {
if (!__proto__) return false
if (__proto__ === prototype) return true
__proto__ = __proto__.__proto__
}
}
function instanceofMock(L, R) {
// L 必须是实例对象
// R 必须是构造函数
if (L === null || typeof R !== 'function') {
return false;
}
while (true) {
// 已经遍历到最顶端
if (L === null) {
return false;
}
if (R.prototype === L.__proto__) {
return true;
}
// 一直顺着 __proto__ 找
L = L.__proto__;
}
}
第一次判断是说,如果传递进来是 null 或者是 第二个参数压根就不是一个function,这种情况,永远都是false了。
while 里面的 ,因为我对L进行了迭代取值,不停的取 protocol,所以这时候 L最终可能会达到 null。