静态方法 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  

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
preventExtensionsChrome Full support 49Edge Full support 12Firefox Full support 42IE No support NoOpera Full support 36Safari Full support 10WebView Android Full support 49Chrome Android Full support 49Edge Mobile Full support YesFirefox Android Full support 42Opera 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

相关链接