如下代码
function Test() {
}
const a = new Proxy({}, {
getPrototypeOf() {
return Array.prototype;
}
});
console.log(a instanceof Array); // true
const b = new Proxy({}, {
getPrototypeOf() {
return Test.prototype;
}
});
console.log(b instanceof Test); // false
console.log(b instanceof Test); // true
console.log(b instanceof Test); // true
为什么 a, b 两个的结果完全不一样, 原生类和我自己定义的类难道有什么区别?
而对于后面两个结果, 难道 getPrototypeOf 存在副作用?
另一个例子如下
class Test {}
const a = new Proxy({}, {
getPrototypeOf() {
return Array.prototype;
}
});
console.log(a instanceof Array); // true
const b = new Proxy({}, {
getPrototypeOf() {
return Test.prototype;
}
});
console.log(b instanceof Test); // true
console.log(b instanceof Test); // true
console.log(b instanceof Test); // true
如果把函数换成 class, 则出现了预期之中的结果, 但是 js 中 class 不是仅仅是语法糖吗? 为什么会有这样的区别?