DEV Community

Discussion on: The Common Enemy, JavaScript's "This" Keyword Saves The Day

 
jochemstoel profile image
Jochem Stoel

Ha! I might actually be incompetent after all. I was not aware of the parseInt optional radix parameter.

As for the context stuff (your code example) honestly this is all so obvious and straightforward. A no-brainer I believe is the expression. Arrow functions don't bind their own this so when you reference this from an arrow function it refers to the parent/originating context. Knowing that, what is the confusion all about?

I don't have to examine it carefully or think deeply about which values change where in what context and why. I have internalized this and it has become trivial and automatic.

Also, this became a bad example when you added window.x at the end. window is not declared anywhere in the snippet. If you are gonna have a debate about context then don't assume a very context specific global object. (it would have been better to put this.x in stead of window.x)
I am not intentionally being a smart-ass pointing this out. You have to understand that a lot of JavaScript does not run in a browser and has no window. For starters this is the case for pretty much all Node modules. Workers don't have a window object either. Javascript as a language doesn't even have a console object but yeah I know that is not the point. I have used many different JavaScript interpreters and written a lot of 'context-free' code so I might be a little obsessive.
Speaking of context specific. Did you know that V8 by itself has no Events? The Event class is written entirely in JavaScript and is not part of the ECMAScript language.

It is important to understand that V8 is only the interpreter. A lot of standard and very common objects/apis that you know do not exist yet. This includes the console object like we have discussed in this article but also the Event class and its children.
More: V8Ception

Oh well. Discussing this I did come to realize that you guys are probably right. Having difficulty understanding scopes and context is probably a lot more normal/common than I thought and I just happen to stand out tremendously in this particular area with my amazing abilities and understanding.

Thread Thread
 
theodesp profile image
Theofanis Despoudis

OK, so what is the value of z then and why? Assume the window is the global object.

The whole question was intentional as its more than the window or the this in an arrow function. Its the combination of all those things plus putting your self in a real working environment with issues to fix and deadlines to hit etc that makes them really tricky.

And yes I'm aware of Node and V8 and the event class (I've written server-side Javascript before)