Call() Method in JavaScript

Ashwings
1 min readDec 15, 2020

“ lets us understand the use of call( ) in javascript ”

image of javascript

Call( ) method is used for using methods of one object in other object

const obj1={        f_name : "xyz",
l_name : "zyx",
print : function(){
console.log(`first Name : ${ this.f_name } ,
last Name : ${this.l_name} `)
}
}obj1.print();const obj2={
f_name : "efg",
l_name : "gfe",
}

Now how to use print() method of obj1 in obj2 ?

To use print method of obj1 in obj2 we use call() method

const obj2={
f_name : "efg",
l_name : "gfe",
}
obj1.print.call(obj2) /* by using call() we used method of obj1 in obj2*/

now lets see how to pass arguments in call() method

Syntax : obj.method().(obj,arguments)

const obj1={f_name : "xyz",
l_name : "zyx",
print : function(title){
console.log(` ${title} . first Name : ${ this.f_name } ,
last Name : ${this.l_name} `)
}
}obj1.print("Mr"); // this method of passing arguments is know const obj2={
f_name : "efg",
l_name : "gfe",
}
obj1.print.call(obj2,"Mr")/* this is how arguments are passed in call() method */

--

--