Saved Bookmarks
| 1. |
Explain call(), apply() and, bind() methods. |
|
Answer» 1. call():
return "Hello " + this.name; } var obj = {name: "Sandy"}; sayHello.call(obj); // Returns "Hello Sandy"
age: 23, getAge: function(){ return this.age; } } var person2 = {age: 54}; person.getAge.call(person2); // Returns 54
return this.name + " is " + message; } var person4 = {name: "John"}; saySomething.call(person4, "awesome"); // Returns "John is awesome" apply() return this.name + " is " + message; } var person4 = {name: "John"}; saySomething.apply(person4, ["awesome"]); 2. bind():
displayDetails: function(registrationNumber,brandName){ return this.name+ " , "+ "bike details: "+ registrationNumber + " , " + brandName; } } var person1 = {name: "Vivek"}; var detailsOfPerson1 = bikeDetails.displayDetails.bind(person1, "TS0122", "Bullet"); // Binds the displayDetails function to the person1 object detailsOfPerson1(); // Returns Vivek, bike details: TS0452, Thunderbird |
|