Tutorialspoint.dev

Object.freeze( ) in JavaScript

Object and Object Constructors in JavaScript?
In the living world of object-oriented programming we already know the importance of classes and objects but unlike other programming languages, JavaScript does not have the traditional classes as seen in other languages. But JavaScript has objects and constructors which work mostly in the same way to perform the same kind of operations.

  • Constructors are general JavaScript functions which are used with the “new” keyword. Constructors are of two types in JavaScript i.e. built-in constructors(array and object) and custom constructors(define properties and methods for specific objects).
  • Constructors can be useful when we need a way to create an object “type” that can be used multiple times without having to redefine the object every time and this could be achieved using the Object Constructor function. It’s a convention to capitalize the name of constructors to distinguish them from regular functions.

For instance, consider the following code:

function Automobile(color) {
  this.color=color;
}

var vehicle1 = new Automobile ("red");

The function “Automobile()” is an object constructor, and its properties and methods i.e “color” is declared inside it by prefixing it with the keyword “this”. Objects defined using an object constructor are then made instants using the keyword “new”.



When new Automobile() is called, JavaScript does two things:

  1. It creates a fresh new object(instance) Automobile() and assigns it to a variable.
  2. It sets the constructor property i.e “color” of the object to Automobile.

Object.freeze() Method
Among the Object constructor methods, there is a method Object.freeze() which is used to freeze an object. Freezing an object does not allow new properties to be added to an object and prevents from removing or altering the existing properties. Object.freeze() preserves the enumerability, configurability, writability and the prototype of the object. It returns the passed object and does not create a frozen copy.

Applications:

  • Object.freeze() is used for freezing objects and arrays.
  • Object.freeze() is used to make an object immutable.

Syntax:



Object.freeze(obj)

Parameters Used:

  1. obj : It is the object which has to be freezed.

Return Value:
Object.freeze() returns the object that was passed to the function.

Examples of the above function are provided below.

Examples:

Input : const obj1 = { property1: 'initial_data'};
        const obj2 = Object.freeze(obj1);
        obj2.property1 = 'new_data';
        console.log(obj2.property1);

Output : "initial_data"

Explanation : In this example, the object “obj2” has been assigned property from object “obj1” and the properties of “obj1” are frozen therefore new property and values are prevented from being added to “obj2”.

Input : var obj = { prop: function() {}, name: 'adam' };
        console.log(obj);
        obj.name = 'billy';
        delete obj.prop;
        console.log(obj);
        var o = Object.freeze(obj);
        obj.name = 'chris';
        console.log(obj);

Output : Object { prop: function () {}, name: "adam" }
         Object { name: "billy" }
         Object { name: "billy" }

Explanation : In this example, the object “obj” has been assigned “prop: function” which has been later deleted since the object “obj wasn’t frozen. After that, a new object “o” has been assigned the frozen values of “obj” which prevented it from further updations.

Codes for the above function are provided below.

Code 1:



<script>
<!-- creating an object constructor and assigning values to it -->
const obj1 = { property1: 'initial_data'};
  
<!--creating a second object which will freeze the properties of the first object-->
const obj2 = Object.freeze(obj1);
  
<!-- Updating the properties of the frozen object -->
obj2.property1 = 'new_data';
  
<!-- Displaying the properties of the  frozen object -->
console.log(obj2.property1);
</script>

OUTPUT :

"initial_data"

Code 2:

<script>
<!-- creating an object constructor and assigning values to it -->
var obj = { prop: function() {}, name: 'adam' };
  
<!-- Displaying the properties of the object created -->
console.log(obj);
  
<!-- Updating the properties of the object -->
obj.name = 'billy';
delete obj.prop;
<!-- Displaying the updated properties of the object -->
console.log(obj);
  
<!-- Freezing the object using object.freeze() method>
var o = Object.freeze(obj);
  
<!-- Updating the properties of the frozen object -->
obj.name = 'chris';
  
<!-- Displaying the properties of the  frozen object -->
console.log(obj);
  
</script>

OUTPUT :

Object { prop: function () {}, name: "adam" }
Object { name: "billy" }
Object { name: "billy" }

Exceptions :

  • It causes a TypeError if the argument passed is not an object .

Reference :https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter