0
0
Javascriptprogramming~5 mins

this in objects in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does this refer to inside a regular object method?
this refers to the object that owns the method. It points to the object before the dot when the method is called.
Click to reveal answer
beginner
How does this behave inside a function called without an object?
In non-strict mode, this refers to the global object (like window in browsers). In strict mode, this is undefined.
Click to reveal answer
intermediate
What happens to this when using arrow functions inside objects?
Arrow functions do not have their own this. They inherit this from the surrounding code where they are defined.
Click to reveal answer
intermediate
How can you explicitly set this when calling a function?
You can use call(), apply(), or bind() methods to set this explicitly.
Click to reveal answer
intermediate
What is a common mistake when using this inside callbacks in objects?
Callbacks often lose the original this context because they are called as regular functions. Using arrow functions or binding this fixes this.
Click to reveal answer
Inside an object method, what does this refer to?
AThe function itself
BThe global object
CUndefined
DThe object owning the method
What does this refer to inside an arrow function defined in an object?
AThe object itself
BThe global object
CThe surrounding context's <code>this</code>
DUndefined
Which method can you use to explicitly set this when calling a function?
Aset()
Bcall()
Crun()
Dexecute()
What happens if you call a regular function (not a method) in strict mode without setting this?
A<code>this</code> is undefined
B<code>this</code> is the function itself
C<code>this</code> is the global object
D<code>this</code> is the object
Why might this lose its expected value inside a callback function?
ABecause callbacks are called as regular functions, losing the original <code>this</code>
BBecause callbacks always use global <code>this</code>
CBecause <code>this</code> is always undefined in callbacks
DBecause callbacks cannot access <code>this</code>
Explain how this works inside an object method and how it differs in arrow functions inside objects.
Think about how the function is called and where arrow functions get their this.
You got /3 concepts.
    Describe ways to control or change the value of this when calling functions in JavaScript.
    Remember the three methods that let you set this explicitly.
    You got /3 concepts.