this, call, apply & bind
Understand the idea
A works as counter.read(), then fails when passed directly to a button. The survived; its intended receiver did not.
A receiver on the call
A shared function may need to read the particular object it is working with. Passing that object as every would be repetitive for a , so JavaScript provides this. The catch is that a regular function's receiver is selected by the call, not permanently by the object where you first saw the function.
Plain calls and method calls
Look at the call site, meaning the that actually invokes the function. The same regular function can be called as a method, as a plain function, or with an explicitly supplied receiver. Inspecting only the function definition leaves out the part that selects this.
In counter.read(), counter supplies the receiver. If you copy read into another and call that variable on its own, the dot is gone and so is that receiver. In , a plain call has as this. Some older non-strict code substitutes the global object; depending on that behavior makes bugs harder to spot.
| Call form | Where this comes from | When the function runs |
|---|---|---|
| plainFunction() in strict mode | undefined | Immediately |
| object.method() | The object before the dot | Immediately |
| Arrow function | Its surrounding this; not the caller | When called |
| fn.call(receiver, a, b) | The explicit receiver | Immediately; individual arguments |
| fn.apply(receiver, args) | The explicit receiver | Immediately; arguments from an array-like value |
| fn.bind(receiver) | Fixed in the returned regular function | Later, when that returned function is called |
Arrows keep the surrounding this
An arrow keeps the surrounding this in the same way that its location fixes access to surrounding names. call, apply, and bind cannot replace that arrow’s this with a different object. Use an arrow when retaining the surrounding receiver is the behavior you want.
An arrow function does not create its own this. It can be useful inside a regular method when a later should keep that method's receiver. An arrow stored directly in an object literal does not inherit the object as this, because the object literal did not establish that surrounding receiver.
Choosing a receiver explicitly
call invokes a regular function with a chosen receiver and individual arguments. apply does the same with an -like collection of arguments. bind returns a new function with its receiver, and optionally some arguments, fixed for later use. It does not invoke the function immediately. These cannot replace an arrow's lexical this.
Passing a method for later
Passing a method to a timer or event listener is a common place to lose the receiver you intended. Read the receiving API’s call rules. It may supply its own receiver, or call the function plainly; it does not know which object your function used to belong to.
A browser listener deserves one extra distinction. When addEventListener invokes a regular listener function, this is the element handling the event, not necessarily your controller object. An arrow created inside your controller's method can keep the controller receiver; bind can fix it explicitly. The example chooses bind so readLater still knows which counter to read.
Mental model
Plain call
read()
Method call
counter.read()
Arrow function
counter.run() → arrow()
Explicit binding
counter.read.call(other)
A plain call in strict mode has an undefined receiver.
01 / 04Plain call
Live example
The direct call and the bound both read 4. Remove bind(counter) and exposes the missing receiver.
Your console output appears here.
Guided challenge
Keep the callback’s receiver
Bind readLater to counter so a later plain call can still read its value.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
4 4
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 150 XP. You can always revisit it.