handler.getOwnPropertyDescriptor()
方法是 Object.getOwnPropertyDescriptor()
的陷阱。
var p = new Proxy(target, { getOwnPropertyDescriptor: function(target, prop) { } });
下列参数会被传入 getOwnPropertyDescriptor
方法中。这是绑定到处理程序。
target
prop
getOwnPropertyDescriptor
方法必须返回一个 object 或 undefined。
handler.getOwnPropertyDescriptor()
方法是 Object.getOwnPropertyDescriptor()
的陷阱。
这个陷阱可以拦截这些操作:
如果下列不变量被违反,代理将抛出一个 TypeError
:
getOwnPropertyDescriptor
必须返回一个 object 或 undefined。
以下是 Object.getOwnPropertyDescriptor()
的代码陷阱:
var p = new Proxy({ a: 20}, { getOwnPropertyDescriptor: function(target, prop) { console.log('called: ' + prop); return { configurable: true, enumerable: true, value: 10 }; } }); console.log(Object.getOwnPropertyDescriptor(p, 'a').value); // "called: a" // 10
以下代码则违反了不变量。
var obj = { a: 10 }; Object.preventExtensions(obj); var p = new Proxy(obj, { getOwnPropertyDescriptor: function(target, prop) { return undefined; } }); Object.getOwnPropertyDescriptor(p, 'a'); // TypeError is thrown
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) [[GetOwnProperty]] |
Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) [[GetOwnProperty]] |
Draft |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | ? | 18 (18) | ? | ? | ? |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | ? | 18.0 (18) | ? | ? | ? |