Chapter XXI · The `this` Keyword
call
The `call` method invokes a function with a specified `this` value and arguments passed individually as a comma-separated list.
01 How It Works
Think of stepping up to a specific microphone podium to speak immediately with assigned credentials.
It immediately executes the target function while explicitly setting the `this` context and passing positional arguments one by one.
example.js
function introduce(lang, city) {
console.log(`I am ${this.name}, speak ${lang} in ${city}`);
}
introduce.call({ name: "Bob" }, "JS", "NYC");
1. Target a function reference.
2. Call `.call(contextObj, arg1, arg2, ...)`.
3. Function executes instantly with explicit bindings.
02 Practical Example
Here is how you might see this concept applied in real-world code:
practical.js
function sum(a, b) { return a + b + this.bonus; }
sum.call({ bonus: 10 }, 5, 5); // 20
Key Takeaway
Use `call` when you need to invoke a function immediately with explicit `this` and individual arguments.