Zastosuj i powiązaj metodę w JavaScript
* call apply and bind
- This concept is called function borrowing
- We can borrow function from the other objects
and use the data with the other object.
-> Call invokes the function and allows you to pass in arguments
one by one.
-> Apply invokes the function and allows you to pass in arguments
as an array.
-> Bind returns a new function, allowing you to pass in a
this array and any number of arguments.
let myName = {
firstname: "Abhishek",
lastname: "Bhavsar",
}
let printFullName = function (hometown, state) {
console.log("=>>>>>>>", this.firstname + " " + this.lastname + "from" + hometown + "," + state)
}
// call
printFullName.call(myName, "Ahmedabad", "Gujrat");
let name2 = {
firstname: "Sachin",
lastname: "Tendulkar",
}
// function borrowing
printFullName.call(name2, "Mumbai", "Maharashtra");
// apply
printFullName.apply(name2, ["Mumbai", "Maharashtra"]);
// bind method
let printMyName = printFullName.bind(name2, "Mumbai", "Maharashtra");
console.log(printMyName);
printMyName();
Abhishek