0
0
Javascriptprogramming~10 mins

this with arrow functions in Javascript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - this with arrow functions
Start function
Arrow function created
Arrow function uses 'this' from surrounding context
Call arrow function
Access 'this' from outer scope
Return or use 'this'
End
Arrow functions do not have their own 'this'. They use 'this' from where they were created.
Execution Sample
Javascript
const obj = {
  name: 'Alice',
  greet: () => console.log(this.name)
};
obj.greet();
This code tries to print 'name' using an arrow function inside an object.
Execution Table
StepCode LineAction'this' valueOutput
1const obj = {...}Create object with name and greetGlobal or module scope
2greet: () => console.log(this.name)Define arrow function; 'this' is from outer scopeGlobal or module scope
3obj.greet()Call arrow functionGlobal or module scopeundefined
4console.log(this.name)Access 'this.name' in arrow functionGlobal or module scopeundefined
💡 'this' inside arrow function is not obj, but outer scope, so this.name is undefined
Variable Tracker
VariableStartAfter 1After 2After 3Final
objundefined{name: 'Alice', greet: [Function]}{name: 'Alice', greet: [Function]}{name: 'Alice', greet: [Function]}{name: 'Alice', greet: [Function]}
thisGlobal or module scopeGlobal or module scopeGlobal or module scopeGlobal or module scopeGlobal or module scope
Key Moments - 2 Insights
Why does 'this.name' inside the arrow function not refer to 'obj.name'?
Because arrow functions do not have their own 'this'. They use 'this' from where they were created, which is the outer/global scope here, not the object.
What would happen if we used a regular function instead of an arrow function for 'greet'?
A regular function's 'this' would refer to the object when called as obj.greet(), so 'this.name' would correctly be 'Alice'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'this' inside the arrow function when obj.greet() is called?
AGlobal or module scope
BUndefined
CThe obj object
DThe greet function itself
💡 Hint
Check the 'this' value column in step 3 and 4 of the execution table.
At which step does the output 'undefined' occur?
AStep 4
BStep 3
CStep 2
DStep 1
💡 Hint
Look at the Output column in the execution table.
If we replace the arrow function with a regular function, how would the output change?
AOutput would be 'undefined' still
BOutput would be an error
COutput would be 'Alice'
DOutput would be an empty string
💡 Hint
Recall that regular functions have their own 'this' bound to the calling object.
Concept Snapshot
Arrow functions do NOT have their own 'this'.
They use 'this' from the surrounding code where they are created.
Inside objects, arrow functions do NOT bind 'this' to the object.
Use regular functions to access object 'this'.
Arrow functions are great for preserving outer 'this' in callbacks.
Full Transcript
This visual trace shows how 'this' behaves inside arrow functions in JavaScript. When an arrow function is created inside an object, it does not get its own 'this'. Instead, it uses 'this' from the outer scope, which is usually the global or module scope. So, when calling obj.greet(), the arrow function's 'this' is not the object, but the outer scope. This causes 'this.name' to be undefined. If we used a regular function instead, 'this' would refer to the object, and 'this.name' would be 'Alice'. This difference is important to understand when choosing between arrow functions and regular functions.