handler.get()
方法用于拦截对象的读取属性操作。
var p = new Proxy(target, { get: function(target, property, receiver) { } });
以下是传递给get方法的参数,this上下文绑定在
handler对象上.
target
property
receiver
get方法可以返回任何值。
handler.get
方法用于拦截对象的读取属性操作。
该方法会拦截目标对象的以下操作:
proxy[foo]和
proxy.bar
Object.create(proxy)[foo]
Reflect.get()
如果违背了以下的约束,proxy会抛出 TypeError
:
以下代码演示如何拦截属性值的读取操作。
var p = new Proxy({}, { get: function(target, prop, receiver) { console.log("called: " + prop); return 10; } }); console.log(p.a); // "called: a" // 10
以下代码演示违反约束的情况。
var obj = {}; Object.defineProperty(obj, "a", { configurable: false, enumerable: false, value: 10, writable: false }); var p = new Proxy(obj, { get: function(target, prop) { return 20; } }); p.a; //会抛出TypeError
Specification | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) [[Get]] |
Standard | Initial definition. |
ECMAScript Latest Draft (ECMA-262) [[Get]] |
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) | ? | ? | ? |