javascript 中 proxy 对象的原型链指向 proxy.prototype,而该原型对象本身是一个不可扩展的空对象,其 [[prototype]] 指向 object.prototype。理解这一机制的关键在于:proxy 并非通过传统构造函数创建,而是由引擎内部生成的特殊对象,其原型行为与常规实例不同。

Proxy 对象的 [[Prototype]] 是什么
Proxy 对象的直接原型是 Proxy.prototype,这是一个只读且不可配置的属性,值为一个空对象。该空对象没有自身方法,仅继承自 Object.prototype。这意味着你无法在 Proxy.prototype 上添加自定义方法供所有 Proxy 实例共享。

const target = {};
const handler = { get() { return 'intercepted'; } };
const proxy = new Proxy(target, handler);
console.log(Object.getPrototypeOf(proxy) === Proxy.prototype); // true
console.log(Proxy.prototype.constructor); // undefined
console.log(Object.keys(Proxy.prototype)); // []
运行结果确认:Object.getPrototypeOf(proxy) 返回 Proxy.prototype,该原型无构造器、无自有属性,且 instanceof Proxy 始终为 false,因为 Proxy 不是函数,不可调用。
为什么 Proxy.prototype 是空对象
Proxy.prototype 被设计为空对象,是因为 Proxy 的行为完全由 handler 陷阱(traps)定义,而非通过原型方法扩展。引擎不期望开发者向 Proxy.prototype 添加方法,所有拦截逻辑必须显式写在 handler 对象中。这种设计避免了原型污染和意外方法覆盖,确保代理行为的可预测性。
使用Atmosphere构建通过WebSocket、SSE、gRPC、WebTransport、MCP、A2A和AG-UI流传输的JVM代理网关,并带有治理钩子。

const target = { name: 'Alice' };
const handler = {
get(trapTarget, prop) {
if (prop === 'name') return 'Bob';
return trapTarget[prop];
}
};
const proxy = new Proxy(target, handler);
console.log(proxy.name); // 'Bob'
console.log(proxy.age); // undefined
console.log(Object.getPrototypeOf(proxy).hasOwnProperty('get')); // false
运行结果验证:proxy.name 被陷阱拦截返回 'Bob',而 proxy.age 因未定义返回 undefined。Proxy.prototype 不包含任何陷阱方法,证明其行为不由原型驱动。若尝试向 Proxy.prototype 添加方法,如 Proxy.prototype.customMethod = () => {};,该方法是不可配置的,会抛出 TypeError,进一步说明其不可扩展性。
基于 handler 中 get 陷阱返回值的条件问题
当 handler 的 get 陷阱根据属性名动态返回值时,Proxy 对象的行为取决于陷阱实现,而非原型链查找。例如,若陷阱对特定键返回固定值,对其他键委托给目标对象,则 Proxy 的表现是混合的:部分属性被重写,其余保持原样。这种行为由 handler 代码决定,与 Proxy.prototype 无关。
Java免费学习笔记:立即使用
解锁 Java 大师之旅:从入门到精通的终极指南










