静态方法 Reflect.preventExtensions() 方法阻止新属性添加到对象 例如:防止将来对对象的扩展被添加到对象中)。该方法与 Object.preventExtensions()相似,但有一些不同点。详情可见 differences。
语法
Reflect.preventExtensions(target)
参数
-
target - 阻止扩展的目标对象。
返回值
返回一个 Boolean 值表明目标对象是否成功被设置为不可扩展。
异常
抛出一个 Object。
描述
Reflect.preventExtensions 方法阻止新属性添加到对象 例如:防止将来对对象的扩展被添加到对象中)。该方法与 Object.preventExtensions() 方法一致。
示例
使用 Reflect.preventExtensions()
详情可见 Object.preventExtensions().
// Objects are extensible by default.
var empty = {};
Reflect.isExtensible(empty); // === true
// ...but that can be changed.
Reflect.preventExtensions(empty);
Reflect.isExtensible(empty); // === false
与 Object.preventExtensions() 的不同点
如果该方法的第一个参数不是一个对象(原始值),那么将造成一个 Object.preventExtensions() 方法, 非对象的第一个参数将被强制转换为对象。
Reflect.preventExtensions(1); // TypeError: 1 is not an object Object.preventExtensions(1); // 1
规范
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) Reflect.preventExtensions |
Standard | Initial definition. |
| ECMAScript Latest Draft (ECMA-262) Reflect.preventExtensions |
Draft |
浏览器兼容性
Update compatibility data on GitHub
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
preventExtensions |
Chrome Full support 49 | Edge Full support 12 | Firefox Full support 42 | 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 42 | 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