Suppose we have an object with properties that we want to re-use for a certain new object.
JavaScript provides a call method that we can use to achieve this objective.
//Sample object
var emp = {
salary_per_day: 10,
monthlySalary: function(){
return this.salary_per_day * 30;
}
};
//call the function
emp.monthlySalary();
> 300
We can use the call method here on the new object to use the method of the existing object.
var emp = {
salary_per_day: 10,
monthlySalary: function(){
return this.salary_per_day * 30;
}
};
var emp2 = {salary_per_day: 50};
emp.monthlySalary.call(emp2);
> 1500
Like this. call function can be utilized to reuse the property of another function.
Top comments (0)