How Inheritance works in Constructor Functions in JavaScript ?
Last Updated :
20 Feb, 2023
Here, we will discuss inheriting a constructor function in JavaScript. Constructor functions define the prototype of the properties an object will contain. Using the constructor function, we can create a new object after passing the required parameters.
Inheriting a previously defined constructor function means using the parameters of the previously defined function along with adding some new parameters to the newly defined constructor function. For this, we need to use the call() function which allows us to call a function defined somewhere else in the current context.
Syntax:
myFunction.call( this, property1, property2, ... , propertyN )
Example: Here, we create an 'Employee' constructor function. A new 'Developer' constructor function is created that will inherit the basic properties of 'Employee' as well as will contain some new properties.
JavaScript
function Employee(name, age, gender, id) {
this.name = name;
this.age = age;
this.gender = gender;
this.id = id;
};
function Developer(name, age, gender, id,
specialization) {
// Calling Employee constructor function
Employee.call(this, name, age, gender, id);
// Adding a new parameter
this.specialization = specialization;
}
console.log(Employee.prototype);
console.log(Developer.prototype);
Output:
Object
constructor: Æ’ Employee(name, age, gender, id)
[[Prototype]]: Object
Object
constructor: Æ’ Developer(name, age, gender, id, specialization)
[[Prototype]]: Object
We can notice that the 'Developer' constructor function inherits the properties of the 'Employee' constructor function along with a new parameter 'specialization'. Here, we called the Employee function using the call() function to pass the required parameters to the Employee constructor function.
We can also create objects using these constructor functions after passing the values of the required properties of that object.
Syntax:
let Obj1 = new Object( parameters )
Example: This example describes the object created with a new keyword to create an instance of an object that has a constructor function.
JavaScript
function Employee(name, age, gender, id) {
this.name = name;
this.age = age;
this.gender = gender;
this.id = id;
};
function Developer(name, age, gender, id, specialization) {
// Calling Employee constructor function
Employee.call(this, name, age, gender, id);
// Adding a new parameter
this.specialization = specialization;
}
// Creating objects
let Employee1 = new Employee("Suraj", 28, "Male", 564);
let Developer1 = new Developer("Karishma", 31, "Female", 345,
"Frontend Developer");
console.log(Employee1);
console.log(Developer1);
Output:
Employee {name: 'Suraj', age: 28, gender: 'Male', id: 564}
age: 28
gender: "Male"
id: 564
name: "Suraj"
[[Prototype]]: Object
Developer {name: 'Karishma', age: 31, gender: 'Female', id: 345,
specialization: 'Frontend Developer'}
age: 31
gender: "Female"
id: 345
name: "Karishma"
specialization: "Frontend Developer"
[[Prototype]]: Object
We can observe that the constructor function of Employee is inherited to create a new constructor function Developer which can be used to create objects with new properties along with the inherited properties of the parent constructor.
Similar Reads
How to make variables private in Constructor functions in JavaScript ? In Javascript there's no specific way to make variables private in a Constructor function. If a variable is genuinely private, meaning it cannot be accessible from the outside. A constructor is a function that generates a new instance of a class, sometimes referred to as an "object." A constructor's
2 min read
What is the (function() { } )() construct in JavaScript? If you've ever played around with JavaScript, you might have seen this expression. It's like a strange set of symbols, but it has a special name that is an immediately invoked function expression, or IIFE. In this article, we will understand each element of the expression as well as the functionalit
3 min read
JavaScript Function() Constructor The JavaScript Function() constructor is used to create new function objects dynamically. By using the Function() constructor with the new operator, developers can define functions on the fly, passing the function body as a string. This allows for greater flexibility in situations where functions ne
2 min read
Default Constructor in JavaScript In JavaScript, a default constructor is not explicitly defined like in some other programming languages such as Java or C++. In JavaScript, objects can be created without a formal constructor. When you create an object using the new keyword along with a constructor function, that function serves as
2 min read
How does JavaScript Hoisting work internally ? In simple terms, hoisting in JavaScript refers to the fact that variables can be used before they're declared. The declared variables are somehow virtually moved to the top of the scope. However, hoisting applies only to variable declarations and not to variable initialization. While almost anyone
4 min read
Interesting Facts about JavaScript Functions Let us talk about some interesting facts about JavaScript Functions that can make you an efficient programmer.Functions Are First-Class CitizensJavaScript treats functions as first-class citizens, meaning they can be:Stored in variablesPassed as arguments to other functionsReturned from other functi
3 min read
What are factory functions in JavaScript ? In JavaScript, a factory function is a function that returns an object. It is a way of creating and returning objects in a more controlled and customizable manner. Factory functions are a form of design pattern that enables the creation of objects with specific properties and behaviors. Why it is us
2 min read
What is a Constructor in JavaScript? A constructor in JavaScript is a special function that is used to create and initialize objects. When we want to create multiple objects with similar properties and methods, the constructor is used as a blueprint to create similar objects. This is useful when you want to create multiple objects with
8 min read
How a Function Returns an Object in JavaScript ? JavaScript Functions are versatile constructs that can return various values, including objects. Returning objects from functions is a common practice, especially when you want to encapsulate data and behavior into a single entity. In this article, we will see how a function returns an object in Jav
3 min read
Explain invoking function in JavaScript In this article, we will learn about invoking the function in Javascript, along with understanding its implementation through examples. Function Invoking is a process to execute the code inside the function when some argument is passed to invoke it. You can invoke a function multiple times by declar
2 min read