Proxy并不是透明代理
在 JavaScript 中,Proxy 可以拦截并自定义对象的基本操作,但它并不是目标对象的透明代理。即使不做任何拦截,Proxy 也无法完全模拟原始对象的行为,这主要源于 this 指向的变化问题。
this 指向问题
1. 方法调用时的 this 指向
const target = {
m() {
console.log(this === proxy); // true 当通过proxy调用时
}
};
const handler = {};
const proxy = new Proxy(target, handler);
target.m(); // false - 直接调用,this指向target
proxy.m(); // true - 通过proxy调用,this指向proxy
2025/6/15大约 4 分钟
