JavaScript Object isFrozen() Method
Last Updated : 19 Jun, 2023
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 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:
JavaScript // 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));
Output:
false
Code 2:
JavaScript // 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));
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
Similar Reads
JavaScript Constructor Method A constructor in JavaScript is a special function used to create and initialize objects. It sets up object properties and is typically invoked using the new keyword. Constructors allow for the creation of multiple instances with similar properties and methods.In JavaScript, constructors can be defin
7 min read
JavaScript Object assign() Method The Object.assign() method is used to copy the values and properties from one or more source objects to a target object. It invokes getters and setters since it uses both [[Get]] on the source and [[Set]] on the target.Syntax:Object.assign(target, ...sources);Parameters:target: It is the target obje
4 min read
JavaScript Object create() Method JavaScript object.create() method is used to create a new object with the specified prototype object and properties. Object.create() method returns a new object with the specified prototype object and properties.Syntax:Object.create(prototype[, propertiesObject])Parameters:prototype: It is the proto
3 min read
JavaScript Object defineProperty() Method The Object.defineProperty() method in JavaScript is a Standard built-in object which defines a new property directly on an object or it can also modify the existing property of an object and return the object. Syntax:Object.defineProperty(obj, prop, descriptor)Parameters:This method accepts three pa
3 min read
JavaScript Object defineProperties() Method The Object.defineProperties() method in JavaScript is a standard built-in Object that defines a new or modifies existing properties directly on an object and it returns the object.Syntax:Object.defineProperties(obj, props) Parameters:Obj: This parameter holds the object on which the properties are g
2 min read
JavaScript Object entries() Method The Object.entries() method in JavaScript is used to retrieve an array of an object's enumerable property [key, value] pairs. This method is particularly useful for transforming and iterating over objects in situations where array-like manipulation is needed.Syntax:Object.entries(obj);Parameters:obj
4 min read
JavaScript Object freeze() Method The Object.freeze() method is used to freeze an object. Freezing an object does not allow new properties to be added to the object and prevents removing or altering the existing properties. Object.freeze() preserves the enumerability, Configurability, writability, and prototype of the object. It ret
3 min read
JavaScript Object getOwnPropertyDescriptor() Method The Object.getOwnPropertyDescriptor() method in JavaScript is a standard built-in object that enables the full information on a property to be accessed and returns a property descriptor for the own property of a given object. Syntax: Object.getOwnPropertyDescriptor( obj, prop ) Parameters: This meth
2 min read
JavaScript Object getOwnPropertyNames() Method The Object.getOwnPropertyNames() method in JavaScript is a standard built-in object which returns all properties that are present in a given object except for those symbol-based non-enumerable properties.Syntax:Object.getOwnPropertyNames(obj)Parameters:This method accepts a single parameter as menti
3 min read
JavaScript Object getOwnPropertySymbols() Method The Object.getOwnPropertySymbols() method in JavaScript is a standard built-in object which returns an array of all symbol properties that are present in a given object. An empty array is returned until symbol properties are set on the object. Syntax: Object.getOwnPropertySymbols(obj) Parameters: ob
2 min read