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 :
- If it is not extensible.
- If all of its properties are non-configurable.
- 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:
- 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 .
Recommended Posts:
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- How to include a JavaScript file in another JavaScript file ?
- this in JavaScript
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.



