Der erste Unterschied zwischen den Methoden call(), apply() und bind() in JavaScript ist ihre Ausführungszeit! call() und apply() sind ähnlich und werden sofort ausgeführt, während bind() eine neue Funktion erzeugt, die wir zu einem späteren Zeitpunkt explizit aufrufen müssen!
Ein weiterer Unterschied ist, dass call() bei der Übergabe von Argumenten die Möglichkeit bietet, diese einzeln und durch Kommas getrennt zu übergeben, apply() erlaubt die Übergabe als Array von Argumenten, und bind() erlaubt beides!
Ich habe den Beispielcode unten angehängt!
const person = {
fullName : function (randomMessage) {
return `Hello, ${this.firstName} ${this.lastName} ${randomMessage}`;
}
}
const personOne = {
firstName : "John",
lastName : "Doe"
}
const personTwo = {
firstName : "Jack",
lastName : "Adhikari"
}
let fullNameBind = person.fullName.bind(personOne, "--Binding");
let fullNameCall = person.fullName.call({firstName : "Sarah", lastName: "Holmes"}, "--Calling");
let fullNameApply = person.fullName.apply(personTwo, ["--Applying"]);
console.log(fullNameBind());
console.log(fullNameCall);
console.log(fullNameApply);