Object.preventExtensions()

Object.preventExtensions() 静态方法防止将新属性添加到对象中(即防止将来对该对象进行扩展)。它还可以防止对象的原型被重新分配。

¥The Object.preventExtensions() static method prevents new properties from ever being added to an object (i.e. prevents future extensions to the object). It also prevents the object's prototype from being re-assigned.

Try it

语法

¥Syntax

js
Object.preventExtensions(obj)

参数

¥Parameters

obj

应使其不可扩展的对象。

返回值

¥Return value

对象变得不可扩展。

¥The object being made non-extensible.

描述

¥Description

如果可以向对象添加新属性,则该对象是可扩展的。Object.preventExtensions() 将对象标记为不再可扩展,因此它永远不会拥有超出其被标记为不可扩展时所具有的属性。请注意,一般情况下,不可扩展对象的属性仍可能被删除。尝试向不可扩展对象添加新属性将会失败,要么默默地失败,要么在 严格模式 中抛出 TypeError

¥An object is extensible if new properties can be added to it. Object.preventExtensions() marks an object as no longer extensible, so that it will never have properties beyond the ones it had at the time it was marked as non-extensible. Note that the properties of a non-extensible object, in general, may still be deleted. Attempting to add new properties to a non-extensible object will fail, either silently or, in strict mode, throwing a TypeError.

Object.seal()Object.freeze() 不同,Object.preventExtensions() 调用固有的 JavaScript 行为,并且不能用多个其他操作的组合来替换。它还有 Reflect 对应项(仅针对内部操作而存在),Reflect.preventExtensions()

¥Unlike Object.seal() and Object.freeze(), Object.preventExtensions() invokes an intrinsic JavaScript behavior and cannot be replaced with a composition of several other operations. It also has its Reflect counterpart (which only exists for intrinsic operations), Reflect.preventExtensions().

Object.preventExtensions() 仅阻止添加自己的属性。属性仍然可以添加到对象原型中。

¥Object.preventExtensions() only prevents addition of own properties. Properties can still be added to the object prototype.

该方法使得目标的 [[Prototype]] 不可变;任何 [[Prototype]] 重新分配都会抛出 TypeError。此行为特定于内部 [[Prototype]] 属性;目标对象的其他属性将保持可变。

¥This method makes the [[Prototype]] of the target immutable; any [[Prototype]] re-assignment will throw a TypeError. This behavior is specific to the internal [[Prototype]] property; other properties of the target object will remain mutable.

一旦对象变得不可扩展,就无法再次使其可扩展。

¥There is no way to make an object extensible again once it has been made non-extensible.

示例

¥Examples

使用 Object.preventExtensions

¥Using Object.preventExtensions

js
// Object.preventExtensions returns the object
// being made non-extensible.
const obj = {};
const obj2 = Object.preventExtensions(obj);
obj === obj2; // true

// Objects are extensible by default.
const empty = {};
Object.isExtensible(empty); // true

// They can be made un-extensible
Object.preventExtensions(empty);
Object.isExtensible(empty); // false

// Object.defineProperty throws when adding
// a new property to a non-extensible object.
const nonExtensible = { removable: true };
Object.preventExtensions(nonExtensible);
Object.defineProperty(nonExtensible, "new", {
  value: 8675309,
}); // throws a TypeError

// In strict mode, attempting to add new properties
// to a non-extensible object throws a TypeError.
function fail() {
  "use strict";
  // throws a TypeError
  nonExtensible.newProperty = "FAIL";
}
fail();

不可扩展对象的原型是不可变的:

¥A non-extensible object's prototype is immutable:

js
const fixed = Object.preventExtensions({});
// throws a 'TypeError'.
fixed.__proto__ = { oh: "hai" };

非对象论证

¥Non-object argument

在 ES5 中,如果该方法的参数不是对象(原语),那么它将导致 TypeError。在 ES2015 中,非对象参数将按原样返回,不会出现任何错误,因为根据定义,原语已经是不可变的。

¥In ES5, if the argument to this method is not an object (a primitive), then it will cause a TypeError. In ES2015, a non-object argument will be returned as-is without any errors, since primitives are already, by definition, immutable.

js
Object.preventExtensions(1);
// TypeError: 1 is not an object (ES5 code)

Object.preventExtensions(1);
// 1                             (ES2015 code)

规范

Specification
ECMAScript Language Specification
# sec-object.preventextensions

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看