首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Reflect.preventExtensions

静态方法Reflect.preventExtensions()方法阻止新属性添加到对象 例如:防止将来对对象的扩展被添加到对象中)。该方法与?Object.preventExtensions()相似,但有一些不同点。详情可见differences

语法

代码语言:javascript
复制
Reflect.preventExtensions(target)

参数

target阻止扩展的目标对象。

返回值

返回一个Boolean值表明目标对象是否成功被设置为不可扩展。

异常

抛出一个TypeError错误,如果对象不是?Object

描述

Reflect.preventExtensions方法阻止新属性添加到对象 例如:防止将来对对象的扩展被添加到对象中)。该方法与??Object.preventExtensions()方法一致。

示例

使用 Reflect.preventExtensions()

代码语言:javascript
复制
// Objects are extensible by default.
var empty = {};
Reflect.isExtensible(empty); // === true

// ...but that can be changed.
Reflect.preventExtensions(empty);
Reflect.isExtensible(empty); // === false

与 to Object.preventExtensions() 的不同点

如果该方法的第一个参数不是一个对象(原始值),那么将造成一个TypeError异常。?对于Object.preventExtensions()方法,?非对象的第一个参数将被强制转换为对象。

代码语言:javascript
复制
Reflect.preventExtensions(1);
// TypeError: 1 is not an object

Object.preventExtensions(1);
// 1

规范

Specification

Status

Comment

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Reflect.preventExtensions' in that specification.

Standard

Initial definition.

ECMAScript Latest Draft (ECMA-262)The definition of 'Reflect.preventExtensions' in that specification.

Draft

?

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

49

(Yes)

42 (42)

No support

No support

10

Feature

Android

Chrome for Android

Edge

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

No support

49

(Yes)

42.0 (42)

No support

No support

10

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com