这篇翻译不完整。请帮忙从英语翻译这篇文章

handler.preventExtensions() 方法用于设置对

Object.preventExtensions()

的拦截

语法

var p = new Proxy(target, {
  preventExtensions: function(target) {
  }
});

参数

以下参数传递给 preventExtensions 方法. 它会绑定到这个handler.

target
所要拦截的目标对象.

返回值

preventExtensions 方法返回一个布尔值.

描述

handler.preventExtensions() 拦截 Object.preventExtensions()返回一个布尔值.

拦截(Interceptions)

这个trap可以拦截这些操作:

约束(Invariants)

如果违反了下列规则, proxy则会抛出一个 TypeError:

  • 如果Object.isExtensible(proxy)falseObject.preventExtensions(proxy)只能返回true

示例

以下代码演示了如何拦截Object.preventExtensions()

var p = new Proxy({}, {
  preventExtensions: function(target) {
    console.log('called');
    Object.preventExtensions(target);
    return true;
  }
});

console.log(Object.preventExtensions(p)); // "called"
                                          // false

以下代码违反了约束.

var p = new Proxy({}, {
  preventExtensions: function(target) {
    return true;
  }
});

Object.preventExtensions(p); // 抛出类型错误

规范

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
[[PreventExtensions]]
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
[[PreventExtensions]]
Draft  

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
Basic supportChrome Full support 49Edge Full support 12Firefox Full support 22IE No support NoOpera Full support 36Safari Full support 10WebView Android Full support 49Chrome Android Full support 49Edge Mobile Full support YesFirefox Android Full support 22Opera Android Full support 36Safari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 6.0.0

Legend

Full support  
Full support
No support  
No support

参考

文档标签和贡献者

标签: 
此页面的贡献者: zxh19890103, xyy94813, varcat
最后编辑者: zxh19890103,