Object.isFrozen( ) In JavaScript

Object and Object Constructor in JavaScript?
In Object Oriented Programming, the ways of defining an object are limiting in many situations. To create an object “type” that can be used multiple times without having to redefine the object every time to meet each particular instance’s needs the standard way is to use the Object Constructor function.
An object constructor is merely a regular JavaScript function, which in general is just as robust i.e define parameters, call other functions etc. Object constructors create the blueprints for objects, not the object itself.
Let us use a real-world item “dog” as an example. A property of a dog may be its color or name and a method may be to “bark”. An important thing to note here is that every dog will have a different name or even bark type. To create an object type that accommodates this need for flexibility, we use an object constructor. So the dog will be an object constructor and its properties(color, name) and methods(bark noise) are declared inside it using “this” keyword. Objects defined using an object constructor are then instantiated using the new keyword.
This helps to easily define multiple instances of dog(an object constructor), each with its own name- that’s the flexibility object constructor brings to custom objects.

Object.isFrozen() Method
Among the Object constructor methods, there is a method Object.isFrozen() which is used to determine if an object is frozen or not.

An object is frozen if all of the below-mentioned conditions hold true :

  1. If it is not extensible.
  2. If all of its properties are non-configurable.
  3. If all its data properties are non-writable.

Object.isFrozen() takes the object as an argument which has to be checked and returns a boolean representing whether the object is frozen or not.

Applications:

  • Object.isfrozen() is used for checking whether an object is frozen or not.

Syntax:

Object.isFrozen(obj)

Parameters Used:

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

Return Value:
Object.isFrozen() returns a boolean representing whether the object is frozen or not.

Examples of the above function are provided below.

Examples:


Input : const object = {
        property: 'hi geeksforgeeks'
        };
        console.log(Object.isFrozen(object));
Output : false

Input : const object = {
        property: 'hi geeksforgeeks'
        };
        Object.freeze(object);
        console.log(Object.isFrozen(object));
Output : true

Codes for the above function are provided below.

Code 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
<!-- creating an object constructor and assigning values to it -->
const object = {
property: 'hi geeksforgeeks'
};
  
<!-- checking whether the object is frozen or not -->
console.log(Object.isFrozen(object));
  
</script>

chevron_right


OUTPUT :

false

Code 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
  
<!-- creating an object constructor and assigning values to it -->
const object = {
property: 'hi geeksforgeeks'
};
  
<!-- Using freeze() method to freeze the object -->
Object.freeze(object);
  
<!-- checking whether the object is frozen or not -->
console.log(Object.isFrozen(object));
  
</script>

chevron_right


OUTPUT :

true

Exceptions :

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


My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.