handler.set() - JavaScript 编辑

The handler.set() method is a trap for setting a property value.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

Syntax

const p = new Proxy(target, {
  set: function(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 Symbol of the property to set.
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", 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.

Return value

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.

Description

The handler.set() method is a trap for setting property value.

Interceptions

This trap can intercept these operations:

  • Property assignment: proxy[foo] = bar and proxy.foo = bar
  • Inherited property assignment: Object.create(proxy)[foo] = bar
  • Reflect.set()

Invariants

If the following invariants are violated, the proxy will throw a TypeError:

  • 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 undefined as its [[Set]] attribute.
  • In strict mode, a false return value from the set() handler will throw a TypeError exception.

Examples

Trap setting of a property value

The following code traps setting a property value.

const p = new Proxy({}, {
  set: function(target, prop, value, receiver) {
    target[prop] = value;
    console.log('property set: ' + prop + ' = ' + value);
    return true;
  }
})

console.log('a' in p);  // false

p.a = 10;               // "property set: a = 10"
console.log('a' in p);  // true
console.log(p.a);       // 10

Specifications

Specification
ECMAScript (ECMA-262)
The definition of '[[Set]]' in that specification.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:146 次

字数:6509

最后编辑:8年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文