Export (0) Print
Expand All

Object.isFrozen Function (JavaScript)

Returns true if existing property attributes and values cannot be modified in an object, and new properties cannot be added to the object.

Object.isFrozen(object)

object

Required. The object to test. This can be a native JavaScript object or a DOM object.

true if all of the following are true:

  • The object is non-extensible, which indicates that new properties cannot be added to the object.

  • The configurable attribute is false for all existing properties.

  • The writable attribute is false for all existing data properties.

If the object has no existing properties, the function returns true if the object is non-extensible.

If the object argument is not an object, a TypeError exception is thrown.

When the configurable attribute of a property is false, the property attributes cannot be changed and the property cannot be deleted. When writable is false, the data property value cannot be changed. When configurable is false and writable is true, the value and writable attributes can be changed.

For information about how to set property attributes, see Object.defineProperty Function (JavaScript). To obtain the attributes of a property, you can use the Object.getOwnPropertyDescriptor Function (JavaScript).

Related Functions

The following related functions prevent the modification of object attributes.

Function

Object is made non-extensible

configurable is set to false for each property

writable is set to false for each property

Object.preventExtensions

Yes

No

No

Object.seal

Yes

Yes

No

Object.freeze

Yes

Yes

Yes

The following functions return true if all of the conditions marked in the following table are true.

Function

Object is extensible?

Object is non-extensible?

configurable is false for all properties?

writable is false for all data properties?

Object.isExtensible

X

Object.isSealed

X

X

Object.isFrozen

X

X

X

The following example illustrates the use of the Object.isFrozen function.

function showFreeze() {

    // Create an object that has two properties.
    var obj = { pasta: "spaghetti", length: 10 };
    
    // Show attributes of the object and its properties.
    showAttributes(obj);

    // Freeze the object.
    Object.freeze(obj);

    // Show the attributes again.
    showAttributes(obj);

    // Try to add a new property, and then verify that it is
    // not added. It is not added because Object.freeze makes the
    // object non-extensible.
    obj.newProp = 50;
    document.write (typeof(obj.newProp) === "undefined");
    document.write (" ");

    // Try to delete a property, and then verify that it is
    // still present. It is still present because Object.freeze sets
    // the property's configurable attribute to false.
    delete obj.length;
    document.write (obj.length === 10);
    document.write (" ");

    // Try to change a property value, and then verify that it
    // is not changed. It is unchanged because Object.freeze sets
    // the data property's writable attribute to false.
    obj.pasta = "linguini";
    document.write (obj.pasta === "spaghetti");
}

function showAttributes(obj)
{
    var newLine = "<br />";

    // Show attributes of the object.
    document.write ("non-extensible: "+ !Object.isExtensible(obj));
    document.write (", isSealed: "+ Object.isSealed(obj));
    document.write (", isFrozen: "+ Object.isFrozen(obj));
    document.write (newLine);

    // Show attributes of each property.
    for (var prop in obj)
    {
        var descriptor = Object.getOwnPropertyDescriptor(obj, prop);

        document.write (prop + " - ");
        document.write ("configurable: " + descriptor.configurable);
        document.write (", writable: " + descriptor.writable);
        document.write(newLine);
    }
    document.write(newLine);
}

The following is the output.

non-extensible: false, isSealed: false, isFrozen: false
pasta - configurable: true, writable: true
length - configurable: true, writable: true

non-extensible: true, isSealed: true, isFrozen: true
pasta - configurable: false, writable: false
length - configurable: false, writable: false

true true true

Supported in the Internet Explorer 9 standards document mode. See Version Information.

Not supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards.

Community Additions

Show:
© 2014 Microsoft