This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

preventExtensions

Summary

Prevents the addition of new properties to an object.

Syntax

Object.preventExtensions( object )
object
Required. The object to make non-extensible.

Return Value

The object that is passed to the function.

Examples

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

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

 // Make the object non-extensible.
 Object.preventExtensions(obj);
 document.write(Object.isExtensible(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);

 // Output:
 // false
 // undefined

Remarks

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.

The following related functions prevent the modification of object attributes.

FunctionObject is made non-extensibleconfigurable is set to false for each propertywritable is set to false for each property
Object.preventExtensionsYesNoNo
Object.sealYesYesNo
Object.freezeYesYesYes

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

FunctionObject is extensible?configurable is false for all properties?writable is false for all data properties?
Object.isExtensibleYesNoNo
Object.isSealedNoYesNo
Object.isFrozenNoYesYes

Exceptions

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

See also

Other articles

Attributions

  • Microsoft Developer Network: Article