手动实现 instanceof

133 阅读1分钟

手动实现 instanceof

function _instanceof(value,type){
	if(typeof value !=='object'||value===null){
    	return false;
    }
    let proto = Object.getPrototypeOf(value);
    while(true){
    	if(proto===null){
        	return false;
        }else if(proto===type.prototype){
        	return true;
        }else{
        	proto = Object.getPrototypeOf(proto);
        }
    }
}

type.prototype我们都知道只有funtion 类型才会有 prototype 这个属性。进一步验证了我们上一篇文章的观点(简要的说,就是 new String 的时候,这个String 是一个构造函数)juejin.cn/post/690898…