Object.isSealed 函数
如果无法在对象中修改现有属性的特性,且无法向对象添加新属性,则返回true。
语法
Object.isSealed(object)
参数
object
必要参数。要测试的对象。
返回值
如果满足下面两个条件,则为true。
- 对象是不可扩展的,这表示无法向对象添加新属性。
- configurable特性对于所有现有属性为false。
在该对象不具有任何属性的情况下,如果该对象是不可扩展的,则此函数返回true。
异常
如果object参数不是对象,则将引发TypeError异常。
备注
在某个属性的configurable特性为false时,无法更改该属性的特性,也无法删除该属性。当writable为false时,无法更改数据属性值。在configurable为false且writable为true时,可以更改value和writable特性。
Object.isScaled函数不使用属性的writable特性来确定其返回值。
欲进一步了解如何设置属性的特性,请参阅Object.defineProperty 函数。若要取得属性的特性,可使用Object.getOwnPropertyDescriptor 函数。
函数
以下相关函数可阻止修改对象的特性。
函数 | 对象已设置为不可扩展的 | 为每个属性将configurable设置为false | 为每个属性将writable设置为false |
---|---|---|---|
Object.preventExtensions | Yes | No | No |
Object.seal | Yes | Yes | No |
Object.freeze | Yes | Yes | Yes |
下面的示例阐释了Object.isScaled函数的用法。
函数 | 函数 | 对象是否可扩展 | 为所有属性将configurable设置为false | 为所有数据属性将writable设置为false |
---|---|---|---|---|
Object.isExtensible | Yes | No | No | |
Object.isSealed | No | Yes | No | |
Object.isFrozen | No | Yes | Yes |
示例
下面的示例演示了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标准模式。