Object.isSealed 函数

如果无法在对象中修改现有属性的特性,且无法向对象添加新属性,则返回true

语法

Object.isSealed(object)

参数

object

必要参数。要测试的对象。

返回值

如果满足下面两个条件,则为true

  • 对象是不可扩展的,这表示无法向对象添加新属性。
  • configurable特性对于所有现有属性为false

    在该对象不具有任何属性的情况下,如果该对象是不可扩展的,则此函数返回true

异常

如果object参数不是对象,则将引发TypeError异常。

备注

在某个属性的configurable特性为false时,无法更改该属性的特性,也无法删除该属性。当writablefalse时,无法更改数据属性值。在configurablefalsewritabletrue时,可以更改valuewritable特性。

Object.isScaled函数不使用属性的writable特性来确定其返回值。

欲进一步了解如何设置属性的特性,请参阅Object.defineProperty 函数。若要取得属性的特性,可使用Object.getOwnPropertyDescriptor 函数

函数

以下相关函数可阻止修改对象的特性。

函数对象已设置为不可扩展的为每个属性将configurable设置为false为每个属性将writable设置为false
Object.preventExtensionsYesNoNo
Object.sealYesYesNo
Object.freezeYesYesYes

下面的示例阐释了Object.isScaled函数的用法。

函数函数对象是否可扩展为所有属性将configurable设置为false为所有数据属性将writable设置为false
Object.isExtensibleYesNoNo
Object.isSealedNoYesNo
Object.isFrozenNoYesYes

示例

下面的示例演示了Object.isSealed函数的用法。

// Create an object that has two properties.
var obj = { pasta: "spaghetti", length: 10 };
// Seal the object, and verify that it is sealed.
Object.seal(obj);
document.write(Object.isSealed(obj));
document.write("<br/>");
// Try to add a new property, and then verify that it is not added.
obj.newProp = 50;
document.write(obj.newProp);
document.write("<br/>");
// Try to delete a property, and then verify that it is still present.
delete obj.length;
document.write(obj.length);
// Output:
// true
// undefined
// 10

必备条件

在以下文档模式中受支持:Internet Explorer 9标准模式、Internet Explorer 10标准模式、Internet Explorer 11标准模式。应用商店应用(Windows 8和Windows Phone 8.1)中也受支持。请参阅版本信息

在以下文档模式中不受支持:怪异模式、Internet Explorer 6标准模式、Internet Explorer 7标准模式、Internet Explorer 8标准模式。

如果你喜欢这篇文章,敬请给站长打赏↑

除特别注明外,本站所有文章均为本站站长原译,转载请注明出处。