DEV Community

loading...
Cover image for Take 'this' Quiz, Understand How 'this' Works in JavaScript

Take 'this' Quiz, Understand How 'this' Works in JavaScript

Annie Liao on February 09, 2020

Among all the abstract ideas JavaScript has to offer, the 'this' keyword can be one of the most challenging concepts to grasp. On the surface, 'thi...
pic
Editor guide
Collapse
pclundaahl profile image
Patrick Charles-Lundaahl

Nice! A quick, well-done test of this knowledge.

My goto whenever anyone asks about this is always You Don't Know JS (github.com/getify/You-Dont-Know-JS...).

Collapse
chiexplores profile image
Chi • Edited

As someone who learned about the self keyword (Smalltalk) and learning about this in JS, the article helps a lot! Especially that arrow function! So does this mean that it is better to create a Class for call instead of just a block variable? That way you can instantiate the Class call.

Collapse
liaowow profile image
Annie Liao Author • Edited

Glad it helps, Chi! And thanks for mentioning Smalltalk -- I had to look it up haha. I don't think there's a preferred way, it all depends on your use case. Also, JavaScript's class keyword is not really a true class as in other object-oriented languages like Smalltalk. I found this deep-dive article, hope it helps as well: scotch.io/tutorials/better-javascr...

Collapse
karataev profile image
Eugene Karataev

Thanks for the fun quiz.
I was able to answer every question correctly thankfully to this post here on dev.


It helps to figure out this value for every possible case in 5 simple steps.
Collapse
liaowow profile image
Annie Liao Author

Loved the flow chart in that post. Thanks, Eugene!

Collapse
akashicseer profile image
akashic seer

I've been programming 15 years in 7+ languages now. Languages change constantly, never memorize anything. Javascript and other languages change as fast as you learn them. Right now I am creating an app in Scala 2 but I will have to rewrite and migrate it within 2 years to Scala 3. So much about the language is changing completely. I have not used PHP in less than 2 years it has changed a lot since. I am moving to physics at least it doesn't change every week LOL

Collapse
nylen profile image
James Nylen

Hi Annie, a couple of small corrections...

In the explanation for examples 2 and 3:

And because the global object does not know anything about say() function, 'this' is undefined.

Should be something like this instead...

And because the global object does not have a caller property, this.caller is undefined.

In the explanation for example 2:

(if you're using a browser console, you might see "null" (Firefox) or "[url referring to the window object]"(Chrome) instead of "undefined")

I tested in Chrome and Firefox and I get undefined in both browsers. This makes sense, because in these examples this.caller is equivalent to window.caller, which is undefined (unless some code on the current page has set this property).

Collapse
liaowow profile image
Annie Liao Author

Thank you, James! Yes. That makes much more sense. I was googling around trying to find the simplest explainer, only to complicate my own thought process. Really appreciate your input :)

Collapse
sambarnes90 profile image
Sam

I actually got 100% on this but I am constantly getting 'this' wrong.

Having it in this format really made me think about where the invocations and definitions were happening.

Really helpful exercise for a relative novice like me - thanks Annie!

Collapse
liaowow profile image
Annie Liao Author

Just gotta keep practicing. Thanks for following along, Sam! Made my day.

Collapse
z2lai profile image
z2lai • Edited

It's good to practice 'this' like this but there's always a whole bunch of other scenarios that might still trip people up. The best thing to do is to have some theory to solidify the understanding:

  1. 'this' is an object created for every Execution Context that is created (on program startup and on every function invocation).
  2. 'this' can change based on how/where the function is called/invoked.
  3. There are four patterns of invoking functions that define the context of the function being called:
  • function invocation (e.g. says())
  • method invocation (e.g. call.says())
  • constructor invocation pattern (when you call a constructor function with the 'new' keyword - e.g. var person = new Caller())
  • apply invocation pattern (when you provide your own context while calling the function with say.call() or say.apply(), or if you explicitly bind the context with say.bind()) Here's a really good article about these four patterns (read until the React part): reactkungfu.com/2015/07/why-and-ho...
Collapse
liaowow profile image
Annie Liao Author

Couldn't agree more. Thanks so much for providing an overarching concept around 'this'. That could've been a better opening for 'this' post (pun intended).

Collapse
keltroth profile image
Django Janny • Edited

Nice !! You should add other tricky ones with something like :

newCall.call(call);

and :

newCall.bind(call)();
Collapse
liaowow profile image
Annie Liao Author

Thanks, Django! I actually thought about that, but then I'd have to introduce call, apply and bind, which might turn this little quiz into a full-fledged code challenge lol.

Collapse
hoffmann profile image
Peter Hoffmann

Just to be nitpicking the correct solution for challenge #4 is not "Undefined called, too!" but "undefined called, too!" (lowercase).
Besides, thanks for the refresh.
The hardest part for me was to remember that an object doesn't have to be new'd to create it's context.

Collapse
liaowow profile image
Annie Liao Author

Haha, good call. Thanks, Peter! That's a good reminder for me too, especially when working on more complex projects.

Collapse
smlka profile image
Andrey Smolko

Hey, there are nice tasks=)
If you have a plan to prepare next episode of this keyword challenges then have a look at my post about this in setTimeout callback:
dev.to/smlka/easyspec-how-does-set...

Collapse
marinesm profile image
Maria Ines Montenegro

Nice quiz! Just had challenge # 2 wrong...helped me realize I didn't have a full understanding of arrow functions

Collapse
liaowow profile image
Annie Liao Author

Thanks Maria! Yeah, that one messed me up too. That’s why I put them side by side. Glad it helps :)

Collapse
justinwozniak profile image
Justin Wozniak

Nice quiz. Nailed it ♥

Collapse
nombrekeff profile image
Keff

Wonderfull test. It made me doubt myself, although I got every one right!! 😝

I learned this mostly from FunFunFunction, I don't know what videos exactly but he made me understand it quite well.

Collapse
hjaintech profile image
Harshit Jain

This was a really great exercise Annie. Thank you for this.

Collapse
adusoft profile image
Geofrey Aduda

Very well put , I always confuse the two! Using arrow and using none when dealing with 'this'

Collapse
armw4 profile image
Antwan R. Wimberly

Thanks 🙏🏼

Collapse
yimammichael profile image
Michael

Thank You.

Collapse
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

On the surface, 'this' seems like an intuitive keyword that simply refers to the environment (or scope) it resides in.

Note: It's actually context, not scope.

Collapse
liaowow profile image
Annie Liao Author

Ah, I see. So context is object-based, and scope is function-based (reference here: ryanmorr.com/understanding-scope-a...). Just corrected it. Thanks so much for clarifying!

Collapse
darryl profile image
Darryl Young

Thanks for sharing, Annie. That was fun.

Collapse
jbk_awesome profile image
Johnbelieve

Thanks for the insight and quiz 😊😊😊

Collapse
alin11 profile image
Ali Nazari

Very nice way of teaching. (Although I answered all correctly 😜😜)

Collapse
anshulnegitc profile image
Anshul Negi

It is good to see the efforts you have put.

Collapse
barrilitodev profile image
Jonathan Blanco Dev

Thanks... great article!

Collapse
jmvallejo profile image
Juan Manuel Vallejo

Very well illustrated 👏

Collapse
anirudhr95 profile image
Anirudh Ramesh

This was pretty great! Thanks :)

Collapse
wanmaoor profile image
wanmaoor

4 of 4, all right.