Tech Study

JavaScript Constructor Function

Javascript constructor Loads the object as soon as the constructor is created. This process is called automatic object initialization. A constructor is a special method that allows an object to initialize itself when it is created.

JavaScript Constructor Function

It Perform automatic object initialization. A constructor has the same name as the class name.A constructor returns an instance of the class itself. So either it doesn’t return anything, or it doesn’t specify a return type, nor does it specify void.

Syntax:

function ConstructorName(param1, param2, ...) 

{

  // constructor function body

}




Example:

// Constructor function definition for Person object

function Person(name, age)

{

this.name = name;

this.age = age;

this.greet = function() 

{

console.log(`Hey, I am ${this.name} and I am ${this.age} years old.`);

};

}

// Create two Person objects using the constructors.

const person1 = new Person(Mohan, 24);

const person2 = new Person(Rohan, 28);

// Call the greeting method on each object

person1.greet();

people2. greet();

Output:

Hey, I am Mohan and I am 24 years old.

Hey, I am Rohan and I am 28 years old.

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more