0
0
Javascriptprogramming~5 mins

this in 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 JavaScript?

In a regular function, this refers to the object that called the function. If no object called it, this is undefined in strict mode or the global object in non-strict mode.

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

An arrow function does not have its own this. Instead, it uses this from the surrounding (lexical) scope where it was defined.

Click to reveal answer
beginner
What is the value of this when a function is called as a method of an object?

When called as a method, this refers to the object before the dot that called the method.

Click to reveal answer
intermediate
What happens to this when you use call() or apply() to invoke a function?

You can explicitly set this by passing the object you want as the first argument to call() or apply(). The function runs with this set to that object.

Click to reveal answer
intermediate
Why does this inside a nested regular function lose the outer this?

Because each regular function has its own this, a nested function called inside another function has this set to undefined in strict mode or the global object in non-strict mode, not the outer function's this. Arrow functions avoid this problem.

Click to reveal answer
Inside a regular function, what does this refer to when called as a method of an object?
AThe global object
BThe function itself
CUndefined
DThe object that called the method
What is special about this inside an arrow function?
AIt is always undefined
BIt uses <code>this</code> from the surrounding scope
CIt refers to the global object
DIt refers to the arrow function itself
What does call() do with this?
ASets <code>this</code> to the first argument passed
BSets <code>this</code> to undefined
CAlways sets <code>this</code> to the global object
DIgnores <code>this</code>
What is the value of this inside a nested regular function?
AUndefined or global object
BSame as outer function's <code>this</code>
CThe nested function itself
DAlways null
Which function type does NOT have its own this?
ARegular function
BConstructor function
CArrow function
DGenerator function
Explain how this behaves differently in regular functions versus arrow functions.
Think about where the function is called versus where it is defined.
You got /4 concepts.
    Describe how you can control the value of this when calling a function.
    Consider methods that let you specify or fix this.
    You got /4 concepts.