Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Object.preventExtensions Function (JavaScript)

Prevents the addition of new properties to an object.

Object.preventExtensions(object)
object

Required. The object to make non-extensible. This can be a native JavaScript object or a DOM object.

The object that is passed to the function.

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

The Object.preventExtensions function makes an object non-extensible, so that new named properties cannot be added to it. After an object is made non-extensible, it cannot be made extensible.

For information about how to set property attributes, see Object.defineProperty 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.preventExtensions function.

function showPreventExtensions() {

    // Create an object that has two properties.
    var obj = { pasta: "spaghetti", length: 10 };

    // Show the extensible attribute of the object.
    showAttribute(obj);

    // Make the object non-extensible.
    Object.preventExtensions(obj);

    // Show the extensible attribute again.
    showAttribute(obj);

    // Try to add a new property, and then verify that it is
    // not added.
    obj.newProp = 50;
    document.write(typeof (obj.newProp) === "undefined");
}

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

    document.write("extensible: " + Object.isExtensible(obj));
    document.write(newLine);
}

Following is the output.

extensible: true
extensible: false
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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft. All rights reserved.