Skip to content

Latest commit

 

History

History
111 lines (87 loc) · 4.5 KB

File metadata and controls

111 lines (87 loc) · 4.5 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
Object.freeze Function (JavaScript) | Microsoft Docs
01/18/2017
windows-client-threshold
devlang-javascript
language-reference
JavaScript
TypeScript
DHTML
Object.freeze function
freeze function
83ffe193-0a37-4e0c-9b66-44c422765fb3
20
mikejo5000
mikejo
ghogen

Object.freeze Function (JavaScript)

Prevents the modification of existing property attributes and values, and prevents the addition of new properties.

Syntax

Object.freeze(object)  

Parameters

object
Required. The object on which to lock the attributes.

Return Value

The object that is passed to the function.

Exceptions

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

Remarks

The Object.freeze function does the following:

  • Makes the object non-extensible, so that new properties cannot be added to it.

  • Sets the configurable attribute to false for all properties of the object. When configurable is false, the property attributes cannot be changed and the property cannot be deleted.

  • Sets the writable attribute to false for all data properties of the object. When writable is false, the data property value cannot be changed.

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

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? configurable is false for all properties? writable is false for all data properties?
Object.isExtensible Yes No No
Object.isSealed No Yes Yes
Object.isFrozen No Yes Yes

Example

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

// Create an object that has two properties.  
var obj = { pasta: "spaghetti", length: 10 };  
  
// Freeze the object.  
Object.freeze(obj);  
  
// 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);  
document.write("<br/>");  
  
// Try to change a property value, and then verify that it is not changed.   
obj.pasta = "linguini";  
document.write(obj.pasta);  
  
// Output:  
// undefined  
// 10  
// spaghetti  
  

Requirements

[!INCLUDEjsv9]

See Also

Object.preventExtensions Function
Object.seal Function
Object.isExtensible Function
Object.isSealed Function
Object.isFrozen Function