C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
JavaScript Object.freeze() MethodThe Object.freeze() method freezes an object that prevents new properties from being added to it. This method prevents the modification of existing property, attributes, and values. Syntax:
Object.freeze(obj) Parameter
Obj: The object to freeze. Return value:This method returns the object that was passed to the function. Browser Support:
Example 1
const object1 = {
property1: 22
};
const object2 = Object.freeze(object1);
object2.property1 = 33;
// Throws an error in strict mode
console.log(object2.property1);
Output: 22 Example 2
const obj1 = { property1: 'freeze'};
const obj2 = Object.freeze(obj1);
obj2.property1 = 'new_data';
console.log(obj2.property1);
Output: " freeze " Example 3
var obj = { prop: function() {}, name: 'charry' };
console.log(obj);
obj.name = 'karri';
delete obj.prop;
console.log(obj);
var o = Object.freeze(obj);
obj.name = 'chris';
console.log(obj);
Output: [object Object] {
name: "charry",
prop: function() {}
}
[object Object] {
name: "karri"
}
[object Object] {
name: "karri"
}
Next TopicJavaScript Objects
|