fe-interview
fe-interview copied to clipboard
手写 instanceof
function instanceof(L, R) {
L = L.__proto__
const O = R.prototype
while(true) {
if (L === null) return false
if (L === O) return true
L = L.__proto__
}
}
左的__proto__存在,是遍历条件,L=== R.prototype 和 L===null 是终止条件
function instance_of(A, B) {
const current = Object.getPrototypeOf(A);
if (current === null) return false;
if (current === B.prototype) return true;
return instance_of(current, B);
}