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 that 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:
- 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 neveropen' }; console.log(Object.isFrozen(object)); Output : false Input : const object = { property: 'hi neveropen' }; Object.freeze(object); console.log(Object.isFrozen(object)); Output : true
Codes for the above function are provided below.
Code 1:
Javascript
// creating an object constructor and assigning values to it const object = { property: 'hi neveropen' }; // checking whether the object is frozen or not console.log(Object.isFrozen(object)); |
Output:
false
Code 2:
Javascript
// creating an object constructor and assigning values to it const object = { property: 'hi neveropen' }; // Using freeze() method to freeze the object Object.freeze(object); // checking whether the object is frozen or not console.log(Object.isFrozen(object)); |
Output:
true
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 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 a 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.
Exceptions: It causes a TypeError if the argument passed is not an object.
Supported browser:
- Google Chrome 6 and above
- Edge 12 and above
- Firefox 4 and above
- Internet Explorer 9
- Opera 12 and above
- Safari 5.1 and above