handler.set()
The handler.set() method is a trap for setting a property
value.
Syntax
new Proxy(target, {
set(target, property, value, receiver) {
}
});
Parameters
The following parameters are passed to the set() method. this
is bound to the handler.
target- : The target object.
property- : The name or
Symbolof the property to set.
- : The name or
value- : The new value of the property to set.
receiver: The object to which the assignment was originally directed. This is usually the proxy itself. But a
set()handler can also be called indirectly, via the prototype chain or various other ways.For example, suppose a script does
obj.name = "jen", andobjis not a proxy, and has no own property.name, but it has a proxy on its prototype chain. That proxy'sset()handler will be called, andobjwill be passed as the receiver.
Return value
The set() method should return a boolean value.
- Return
trueto indicate that assignment succeeded. - If the
set()method returnsfalse, and the assignment happened in strict-mode code, aTypeErrorwill be thrown.
Description
The handler.set() method is a trap for setting property
value.
Interceptions
This trap can intercept these operations:
- Property assignment:
proxy[foo] = barandproxy.foo = bar Reflect.set()
Or any other operation that invokes the [[Set]] internal method.
Invariants
If the following invariants are violated, the trap throws a TypeError when invoked.
- Cannot change the value of a property to be different from the value of the corresponding target object property if the corresponding target object property is a non-writable, non-configurable data property.
- Cannot set the value of a property if the corresponding target object property is a
non-configurable accessor property that has
undefinedas its[[Set]]attribute. - In strict mode, a
falsereturn value from theset()handler will throw aTypeErrorexception.