handler.ownKeys() 方法用于拦截 Reflect.ownKeys().
JavaScript Demo: handler.ownKeys()
const monster1 = {
_age: 111,
[Symbol("secret")]: "I am scared!",
eyeCount: 4,
};
const handler1 = {
ownKeys(target) {
return Reflect.ownKeys(target);
},
};
const proxy1 = new Proxy(monster1, handler1);
for (const key of Object.keys(proxy1)) {
console.log(key);
// Expected output: "_age"
// Expected output: "eyeCount"
}
语法
var p = new Proxy(target, {
ownKeys: function (target) {},
});
参数
下面的参数被传递给 ownKeys。this 被绑定在 handler 上。
target- : 目标对象。
返回值
ownKeys 方法必须返回一个可枚举对象。
描述
handler.ownKeys() 方法用于拦截 Reflect.ownKeys().
拦截
该拦截器可以拦截以下操作::
- Object.getOwnPropertyNames()
- Object.getOwnPropertySymbols()
- Object.keys()
- Reflect.ownKeys()
约束
如果违反了下面的约束,proxy 将抛出错误 TypeError:
ownKeys的结果必须是一个数组。- 数组的元素类型要么是一个 String ,要么是一个 Symbol.
- 结果列表必须包含目标对象的所有不可配置(non-configurable)、自有(own)属性的 key.
- 如果目标对象不可扩展,那么结果列表必须包含目标对象的所有自有(own)属性的 key,不能有其他值。
示例
下面的代码拦截 Object.getOwnPropertyNames().
var p = new Proxy(
{},
{
ownKeys: function (target) {
console.log("called");
return ["a", "b", "c"];
},
},
);
console.log(Object.getOwnPropertyNames(p)); // "called"; outputs [ 'a', 'b', 'c' ]
下面的代码违反了约定
var obj = {};
Object.defineProperty(obj, "a", {
configurable: false,
enumerable: true,
value: 10,
});
var p = new Proxy(obj, {
ownKeys: function (target) {
return [123, 12.5, true, false, undefined, null, {}, []];
},
});
console.log(Object.getOwnPropertyNames(p));
// TypeError: proxy [[OwnPropertyKeys]] 必须返回一个数组
// 数组元素类型只能是 String 或 Symbol
规范
浏览器兼容性
参见
- Proxy
- Proxy/Proxy
- Object.getOwnPropertyNames()
- Reflect.ownKeys()
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
