Tutorialspoint.dev

Object.isSealed( ) In JavaScript

Object and Object Constructors in JavaScript?
In the living world of object-oriented programming we already know the importance of classes and objects but unlike other programming languages, JavaScript does not have the traditional classes as seen in other languages. But JavaScript has objects and constructors which work mostly in the same way to perform the same kind of operations.

  • Constructors are general JavaScript functions which are used with the “new” keyword. Constructors are of two types in JavaScript i.e. built-in constructors(array and object) and custom constructors(define properties and methods for specific objects).
  • Constructors can be useful when we need a way to create an object “type” that can be used multiple times without having to redefine the object every time and this could be achieved using the Object Constructor function. It’s a convention to capitalize the name of constructors to distinguish them from regular functions.

For instance, consider the following code:

function Automobile(color) {
  this.color=color;
}
  
var vehicle1 = new Automobile ("red");

The function “Automobile()” is an object constructor, and its properties and methods i.e “color” is declared inside it by prefixing it with the keyword “this”. Objects defined using an object constructor are then made instants using the keyword “new”.

When new Automobile() is called, JavaScript does two things:

  1. It creates a fresh new object(instance) Automobile() and assigns it to a variable.
  2. It sets the constructor property i.e “color” of the object to Automobile.

Object.isSealed() Method
Object.isSealed() method is used to determine if an object is sealed or not.
An object is sealed 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 it is not removable (but not necessarily non-writable).

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

Applications:

  • Object.isSealed() is used for checking whether an object is sealed or not.

Syntax:

Object.isSealed(obj)

Parameters Used:
obj : It is the object which has to be checked.
Return Value:
Object.isSealed() returns a boolean representing whether the object is sealed or not.

Examples of the above function are provided below.

Examples:

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

Explanation: In the above example the object has not been sealed using the Object.seal() method, therefore, it returns false when it is checked using Object.isSealed() method.

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

Explanation: In the above example the object has been sealed using the Object.seal() method, therefore, it returns true when it is checked using Object.isSealed() method.

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 sealed or not 
console.log(Object.isSealed(object));
  
</script>

OUTPUT :

false

Code 2:

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

OUTPUT :

true

Exceptions :

  • It causes a TypeError if the argument passed is not an object .
  • If an object is not passed as an argument to the method, then it treats it as a sealed object and returns true.


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter