The handler.set() method is a trap for setting a property value.
var p = new Proxy(target, {
set: function(target, property, value, receiver) {
}
});
The following parameters are passed to the set method. this is bound to the handler.
targetpropertyvaluereceiverset handler can also be called indirectly, via the prototype chain or various other ways.obj.name = "jen", and obj is not a proxy, and has no own property .name, but it has a proxy on its prototype chain. That proxy's set handler will be called, and obj will be passed as the receiver.The set method should return a boolean value. Return true to indicate that assignment succeeded. If the set method returns false, and the assignment happened in strict-mode code, a TypeError will be thrown.
The handler.set method is a trap for setting property value.
This trap can intercept these operations:
proxy[foo] = bar and proxy.foo = bar
Object.create(proxy)[foo] = bar
Reflect.set()If the following invariants are violated, the proxy will throw a TypeError:
undefined as its [[Set]] attribute.false return value from the set handler will throw a TypeError exception.The following code traps setting a property value.
var p = new Proxy({}, {
set: function(target, prop, value, receiver) {
console.log('called: ' + prop + ' = ' + value);
return true;
}
});
p.a = 10; // "called: a = 10"
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of '[[Set]]' in that specification. | Standard | Initial definition. |
| ECMAScript 2017 Draft (ECMA-262) The definition of '[[Set]]' in that specification. | 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) | ? | ? | ? |
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/handler/set