静态方法 Reflect.preventExtensions() 方法阻止新属性添加到对象 例如:防止将来对对象的扩展被添加到对象中)。该方法与 Object.preventExtensions()相似,但有一些不同点。详情可见 differences。
Reflect.preventExtensions(target)
target返回一个 Boolean 值表明目标对象是否成功被设置为不可扩展。
抛出一个 TypeError 错误,如果对象不是 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() 的不同点如果该方法的第一个参数不是一个对象(原始值),那么将造成一个 TypeError 异常。 对于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 |
| 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 | Edge Mobile Full support Yes | 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 |