Javascript : Call method and Function Borrowing
The call method is built in Javascript function that allows to call a function with specific ‘this’ value in arguments provided individually. It enables the explicit setting of ‘this’ inside a function and passing arguments in it.
The call method and apply method is basic difference is passing arguments.
The call method takes an arguments individually. The apply method take an argument as an array.
call method is used to define to borrow the function which is called function borrowing.
For an example :
let programme= {
language: "Hello",
message: "Javascript is synchronous programming."
printFullLangauge: function() {
console.log(this.langauge + " " + this.message);
}
}
//programme.printFullLangauge (prints "Hello Javascript is synchronous programming.")
let programmeTwo = {
name : "HI",
address: "Javascript"
}
//function borrowing
programme.printFullLangauge.call(programmeTwo);
(Prints the "HI Javascript");
also we can declare a method outside of the object too;
let programme= {
language: "Hello",
message: "Javascript is synchronous programming."
}
let printFullLangauge = function() {
console.log(this.langauge + " " + this.message);
}
//programme.printFullLangauge (prints "Hello Javascript is synchronous programming.")
let programmeTwo = {
name : "HI",
address: "Javascript"
}
//function borrowing
printFullLangauge.call(programmeTwo);
(Prints the "HI Javascript");