Object.create 与instanceof 原理

Object.create 方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__。
Object.create = function(proto){
    
    
	var foo = function(){
    
    }
    foo.prototype = proto
    return new foo()
}
instanceof 运算符用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上
function instanceof(l,r){
    
    
	if(typeof l !=== 'object' || l === null) return false;
	r = right.prototype
	l = l.__proto__
	while(true){
    
    
		//查到尽头,没找到
		if(l === null) return false
		//找到相同的原型对象
		if(l === r) return true
		l = l.__proto__
	}
}

猜你喜欢

转载自blog.csdn.net/m0_37285193/article/details/115740761