Tutorialspoint.dev

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:

<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>

OUTPUT :

false

Code 2:

<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>

OUTPUT :

true

Exceptions :

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


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter