this with arrow functions in Javascript - Time & Space Complexity
Let's explore how the use of this inside arrow functions affects the number of operations in JavaScript code.
We want to see how the way this works with arrow functions changes the work done as input grows.
Analyze the time complexity of the following code snippet.
const obj = {
values: [1, 2, 3, 4, 5],
multiply(factor) {
return this.values.map(x => x * factor);
}
};
const result = obj.multiply(2);
console.log(result);
This code multiplies each number in an array by a factor using an arrow function inside map.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
mapmethod loops through each element of thevaluesarray. - How many times: It runs once for each item in the array, so as many times as the array length.
As the array gets bigger, the number of times the arrow function runs grows directly with the array size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to the arrow function |
| 100 | 100 calls to the arrow function |
| 1000 | 1000 calls to the arrow function |
Pattern observation: The work grows evenly as the input size grows.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of items in the array.
[X] Wrong: "Using an arrow function changes how many times the loop runs or makes it faster."
[OK] Correct: The arrow function only changes how this behaves inside it, not how many times the loop runs or the overall work done.
Understanding how this works with arrow functions helps you write clearer code and explain your choices confidently in interviews.
What if we replaced the arrow function with a regular function inside map? How would the time complexity change?
Practice
What is true about this in arrow functions compared to regular functions?
Solution
Step 1: Understand how
Arrow functions do not have their ownthisworks in arrow functionsthis. Instead, they use thethisvalue from the surrounding (lexical) context where they are created.Step 2: Compare with other options
Options B, C, and D are incorrect because arrow functions do not bindthisto the global object, undefined, or themselves.Final Answer:
thisin arrow functions is inherited from the surrounding code where they are created. -> Option AQuick Check:
thisin arrow functions = lexicalthis[OK]
this, not their own [OK]- Thinking arrow functions have their own
this - Confusing arrow functions with regular functions
- Assuming
thisis always global in arrow functions
Which of the following is the correct syntax to define an arrow function that logs this.name?
const obj = { name: 'Alice', logName: ??? }Solution
Step 1: Identify arrow function syntax
Arrow functions use the syntax() => expressionor() => { statements }. logName: () => console.log(this.name) uses the correct concise syntax.Step 2: Check other options
Options B and D use regularfunction()syntax, not arrow functions. logName: () { console.log(this.name) }() { console.log(this.name) }is missing the fat arrow=>, resulting in a syntax error. Note: even with correct arrow syntax as an object method,thiswould refer to the lexical scope (global), not the object.Final Answer:
logName: () => console.log(this.name) -> Option DQuick Check:
Arrow function syntax = logName: () => console.log(this.name) [OK]
- Forgetting the fat arrow '=>'
- Using function keyword instead of arrow syntax
- Confusing arrow function with method shorthand
What will be the output of this code?
const person = {
name: 'Bob',
greet: () => {
console.log(this.name);
}
};
person.greet();Solution
Step 1: Understand
The arrow functionthisin arrow functions inside objectsgreetdoes not have its ownthis. It usesthisfrom the surrounding scope, which is the global scope here.Step 2: Check global
In most environments,this.namethis.namein global scope isundefinedunless explicitly set.Final Answer:
undefined -> Option AQuick Check:
Arrow functionthis= lexicalthis(global) [OK]
this, not object [OK]- Expecting
thisto be the object - Confusing arrow function with regular method
- Assuming global
this.nameis set
Find the error in this code and fix it so that this.name inside greet refers to the user object:
const user = {
name: 'Carol',
greet: () => {
console.log(this.name);
}
};
user.greet();Solution
Step 1: Identify why
Arrow functions do not have their ownthis.nameis incorrectthis, so insidegreet,thisis not theuserobject.Step 2: Fix by using a regular function method
Defininggreetas a regular function method bindsthisto theuserobject when called asuser.greet().Final Answer:
Changegreetto a regular function:greet() { console.log(this.name); }-> Option CQuick Check:
Use regular function for object methods needingthis[OK]
this [OK]- Trying to bind
thisto arrow functions - Using arrow functions for object methods expecting
this - Accessing object properties via global
this
You want to create an object with a method that uses a callback to print the object's name property after 1 second. Which code correctly uses arrow functions to keep this referring to the object?
const obj = {
name: 'Dave',
printName: function() {
setTimeout(??? , 1000);
}
};
obj.printName();Solution
Step 1: Understand
InsidethisinsidesetTimeoutcallbacksetTimeout, a regular function'sthisis global or undefined, so it loses the object context.Step 2: Use arrow function to inherit
Using an arrow function as callback keepsthisfromprintNamethisfromprintName, which is the objectobj.Final Answer:
() => console.log(this.name) -> Option BQuick Check:
Arrow function callback keepsthisfrom method [OK]
this context [OK]- Using regular function losing
thiscontext - Referencing object by name inside callback (less flexible)
- Not using arrow function for callbacks needing
this
Ready to prove it?
Pass the test and this pattern moves unknown
