Object.seal()

Object.seal() 静态方法密封一个对象。密封对象 防止扩展 并使现有属性不可配置。密封对象具有一组固定的属性:无法添加新属性,无法删除现有属性,无法更改其可枚举性和可配置性,并且无法重新分配其原型。只要现有属性可写,它们的值就仍然可以更改。seal() 返回与传入的对象相同的对象。

¥The Object.seal() static method seals an object. Sealing an object prevents extensions and makes existing properties non-configurable. A sealed object has a fixed set of properties: new properties cannot be added, existing properties cannot be removed, their enumerability and configurability cannot be changed, and its prototype cannot be re-assigned. Values of existing properties can still be changed as long as they are writable. seal() returns the same object that was passed in.

Try it

语法

¥Syntax

js
Object.seal(obj)

参数

¥Parameters

obj

应密封的对象。

返回值

¥Return value

被封印的对象。

¥The object being sealed.

描述

¥Description

密封一个对象相当于 防止扩展,然后将所有现有的 属性的描述符 更改为 configurable: false。这具有使对象的属性集固定的效果。使所有属性不可配置还会阻止它们从数据属性转换为访问器属性,反之亦然,但不会阻止更改数据属性的值。尝试删除或添加属性到密封对象,或者将数据属性转换为访问器(反之亦然)都会失败,要么默默地失败,要么抛出 TypeError(最常见,但不排除,当在 strict mode 代码中时)。

¥Sealing an object is equivalent to preventing extensions and then changing all existing properties' descriptors to configurable: false. This has the effect of making the set of properties on the object fixed. Making all properties non-configurable also prevents them from being converted from data properties to accessor properties and vice versa, but it does not prevent the values of data properties from being changed. Attempting to delete or add properties to a sealed object, or to convert a data property to accessor or vice versa, will fail, either silently or by throwing a TypeError (most commonly, although not exclusively, when in strict mode code).

私有属性 没有属性描述符的概念。无论对象是否密封,都无法在对象中添加或删除私有属性。

¥Private properties do not have the concept of property descriptors. Private properties cannot be added or removed from the object, whether the object is sealed or not.

原型链保持不变。然而,由于 防止扩展 的影响,[[Prototype]] 不能被重新分配。

¥The prototype chain remains untouched. However, due to the effect of preventing extensions, the [[Prototype]] cannot be reassigned.

Object.freeze() 不同,用 Object.seal() 密封的对象可以改变其现有属性,只要它们是可写的。

¥Unlike Object.freeze(), objects sealed with Object.seal() may have their existing properties changed, as long as they are writable.

示例

¥Examples

使用 Object.seal

¥Using Object.seal

js
const obj = {
  prop() {},
  foo: "bar",
};

// New properties may be added, existing properties
// may be changed or removed.
obj.foo = "baz";
obj.lumpy = "woof";
delete obj.prop;

const o = Object.seal(obj);

o === obj; // true
Object.isSealed(obj); // true

// Changing property values on a sealed object
// still works.
obj.foo = "quux";

// But you can't convert data properties to accessors,
// or vice versa.
Object.defineProperty(obj, "foo", {
  get() {
    return "g";
  },
}); // throws a TypeError

// Now any changes, other than to property values,
// will fail.
obj.quaxxor = "the friendly duck";
// silently doesn't add the property
delete obj.foo;
// silently doesn't delete the property

// ...and in strict mode such attempts
// will throw TypeErrors.
function fail() {
  "use strict";
  delete obj.foo; // throws a TypeError
  obj.sparky = "arf"; // throws a TypeError
}
fail();

// Attempted additions through
// Object.defineProperty will also throw.
Object.defineProperty(obj, "ohai", {
  value: 17,
}); // throws a TypeError
Object.defineProperty(obj, "foo", {
  value: "eit",
}); // changes existing property value

非对象论证

¥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.seal(1);
// TypeError: 1 is not an object (ES5 code)

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

规范

Specification
ECMAScript Language Specification
# sec-object.seal

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看