handler.preventExtensions()
方法用于设置对Object.preventExtensions()
的拦截
语法
var p = new Proxy(target, { preventExtensions: function(target) { } });
参数
以下参数传递给 preventExtensions
方法. 它会绑定到这个handler.
-
target
- 所要拦截的目标对象.
返回值
preventExtensions
方法返回一个布尔值.
描述
handler.preventExtensions()
拦截 Object.preventExtensions()
返回一个布尔值.
拦截
这个trap可以拦截这些操作:
约束
如果违反了下列规则, proxy则会抛出一个 TypeError
:
- 如果
Object.isExtensible(proxy)
是false
,Object.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 |
浏览器兼容性
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out
https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
preventExtensions |
Chrome Full support 49 | Edge Full support 12 | Firefox Full support 22 | IE No support No | Opera Full support 36 | Safari Full support 10 | WebView Android Full support 49 | Chrome Android Full support 49 | Firefox Android Full support 22 | Opera Android Full support 36 | Safari iOS Full support 10 | Samsung Internet Android Full support 5.0 | nodejs Full support 6.0.0 |
Legend
- Full support
- Full support
- No support
- No support