0
0
Javascriptprogramming~5 mins

this with arrow functions in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does this refer to inside a regular function?

In a regular function, this refers to the object that called the function. If called alone, it can be undefined in strict mode or the global object otherwise.

Click to reveal answer
beginner
How does this behave inside an arrow function?

Inside an arrow function, this does NOT have its own value. It uses this from the surrounding (parent) scope where the arrow function was created.

Click to reveal answer
intermediate
Why might arrow functions be useful when working with this?

Arrow functions keep the this from the outer scope, so you don’t have to use tricks like var self = this or bind(). This makes code simpler and easier to understand.

Click to reveal answer
intermediate
Can arrow functions be used as methods in objects? What happens to this?

Arrow functions should NOT be used as methods because this will not refer to the object but to the outer scope. This usually causes unexpected behavior.

Click to reveal answer
beginner
Example: What will this code print?<br><pre>const obj = {<br>  value: 42,<br>  regularFunc: function() {<br>    console.log(this.value);<br>  },<br>  arrowFunc: () => {<br>    console.log(this.value);<br>  }<br>};<br>obj.regularFunc();<br>obj.arrowFunc();</pre>

The regularFunc prints 42 because this refers to obj. The arrowFunc prints undefined because this refers to the outer scope (likely the global object or undefined in strict mode) which has no value.

Click to reveal answer
What does this inside an arrow function refer to?
AUndefined always
BThe global object always
CThe object that called the function
DThe <code>this</code> of the surrounding scope
Which is true about arrow functions as object methods?
AThey correctly bind <code>this</code> to the object
BThey do not have their own <code>this</code> and use outer <code>this</code>
CThey always refer to the global object
DThey throw an error when called
Why might you choose an arrow function inside a callback to keep this?
ABecause arrow functions always refer to the global object
BBecause arrow functions create a new <code>this</code>
CBecause arrow functions inherit <code>this</code> from the parent scope
DBecause arrow functions do not allow <code>this</code>
What will this code print?<br>
const obj = {<br>  count: 10,<br>  increment: function() {<br>    setTimeout(() => {<br>      console.log(this.count);<br>    }, 100);<br>  }<br>};<br>obj.increment();
A10
BError
Cundefined
D0
Which of these is NOT true about arrow functions?
AThey always bind <code>this</code> to the global object
BThey cannot be used as constructors
CThey do not have their own <code>this</code>
DThey inherit <code>this</code> from the parent scope
Explain how this behaves differently in arrow functions compared to regular functions.
Think about where the function is created versus where it is called.
You got /3 concepts.
    Describe a situation where using an arrow function helps avoid common mistakes with this.
    Consider asynchronous code like setTimeout or event handlers.
    You got /3 concepts.