What is the difference between call, apply and bind?
function hello(greeting, mark) {
console.log(greeting + ", " + this.name + mark);
}
const person = { name: "Maria" };
hello.call(person, "Hi", "!");
hello.apply(person, ["Hello", "?"]);
const bound = hello.bind(person, "Hey");
bound(".");
Output:
Tutorials Class - Output Window
Hi, Maria!
Hello, Maria?
Hey, Maria.
| Method | Runs now? | Arguments |
|---|---|---|
call | Yes | One by one |
apply | Yes | As an array |
bind | No, returns a new function | One by one, can be set in advance |
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |