Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate20 minLESSON 14 / 34

this, call, apply & bind

01

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.

Six call forms and their receivers
Call formWhere this comes fromWhen the function runs
plainFunction() in strict modeundefinedImmediately
object.method()The object before the dotImmediately
Arrow functionIts surrounding this; not the callerWhen called
fn.call(receiver, a, b)The explicit receiverImmediately; individual arguments
fn.apply(receiver, args)The explicit receiverImmediately; arguments from an array-like value
fn.bind(receiver)Fixed in the returned regular functionLater, 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.

02

Mental model

Plain call

read()

undefined

Method call

counter.read()

counter

Arrow function

counter.run() → arrow()

counter

Explicit binding

counter.read.call(other)

other

A plain call in strict mode has an undefined receiver.

01 / 04Plain call

03

Live example

The direct call and the bound both read 4. Remove bind(counter) and exposes the missing receiver.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Keep the callback’s receiver

Bind readLater to counter so a later plain call can still read its value.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

4
4

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What does bind return?
    02Can call replace an arrow’s lexical this?

    Ready to call this one understood?

    Mark this lesson complete to earn 150 XP. You can always revisit it.

    Sign in to save progress
    Next lesson