Skip to the workspace
Pebellslearn by building
Using (this) keyword  ›  Lesson

this in Arrow Functions

in arrow functions 12 min

javascript / this in Arrow Functions

Explore each scene at your own pace.

01 · this in Arrow Functions

this in Arrow Functions

this in Arrow Functions

Arrow functions resolve this from their surrounding lexical environment. Calling an arrow with call, apply or bind does not replace that lexical this.

Use an arrow to retain an enclosing receiver deliberately, not to declare every object method.

See the mechanism

Walk through the modelthis in Arrow FunctionsAuthored snapshots of the example; use Run below to test the code.
makeReader thisarticle
The ordinary method receives the article receiver.
1 of 3

Follow one example

javascript

makeReader is an ordinary method called with article as this. Its returned arrow uses that surrounding this, so both calls read Morning. The later call receiver does not override it. An arrow written directly in an object literal would instead capture the literal’s surrounding this, not the object by virtue of being a property.

Predict, change and check

Predict: What changes if the returned arrow becomes an ordinary function and is called with the other receiver?

Test your explanation: Make that replacement and use .call({title: "Night"}). Compare definition-time lexical access with call-time receiver selection.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

Lexical this is a captured relationship

An arrow reads this from its surrounding lexical environment. call, apply and bind cannot replace that arrow's this value as they can for an ordinary function. An arrow property in an object literal does not automatically capture the object literal as a receiver.

An arrow created inside an instance method or field initialization can deliberately retain the instance context, but it may also create per-instance function objects. Choose the relationship your API needs, not a blanket rule that arrows are always more modern methods. Test the same callback after detaching it from its original owner.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.