4 ms·
It has nothing to do with dynamic binding. "this" has lexical scope just like regular parameters. I agree 'this' is confusing, but you are making it sound more
by goto11 7y ago
It has nothing to do with dynamic binding. "this" has lexical scope just like regular parameters. I agree 'this' is confusing, but you are making it sound more mysterious than it is. It is just some syntactic sugar around an implicit first parameter.
- DonHopkins 7y agoNope. "this" is formally defined as a keyword in the JavaScript syntax, not a variable, not a property name, not an identifier, and not syntactic sugar, since there is no source level transformation you can make that behaves the same (like with C++, the way CFront translated "this" into an actual first parameter). JavaScript's "this" is magic, that's why it has to be a keyword, not a variable. It looks like a variable, and it kind of but not really behaves like an implicit first parameter in some circumstances, but that's not really what's going on at all. And that is the danger, and source of misunderstanding and bugs: it looks like something that it's not. For one thing, you can't assign to "this" like a variable. For another thing, "this" doesn't show up in "arguments" (which is also magic). If you think "this" is not mysterious and just a normal but implicit argument, you don't really understand it. Which is my point: "this" is such a terribly designed language feature that even experienced JavaScript programmers misunderstand it, and it causes subtle hard to find bugs, even for experienced JavaScript developers. It's not just mysterious, it's dangerous. https://books.google.nl/books?id=4RChxt67lvwC&pg=PA169&lpg=PA169&dq=%22Note+that+this+is+a+keyword,+not+a+variable+or+property+name.+JavaScript+syntax+does+not+allow+you+to+assign+a+value+to+this.%22&source=bl&ots=ti16DhFWo9&sig=ACfU3U3PKQUh_RnM1VwI4DZYXPAsxz8lyg&hl=en&sa=X&ved=2ahUKEwjoyO-56L3oAhWFyqQKHV5ED2AQ6AEwAHoECAoQAQ#v=onepage&q=%22Note%20that%20this%20is%20a%20keyword%2C%20not%20a%20variable%20or%20property%20name.%20JavaScript%20syntax%20does%20not%20allow%20you%20to%20assign%20a%20value%20to%20this.%22&f=false https://books.google.nl/books?id=4RChxt67lvwC&pg=PA169&lpg=P... >JavaScript: The Definitive Guide >Note that this is a keyword, not a variable or property name. JavaScript syntax does not allow you to assign a value to this. >Unlike variables, the this keyword does not have a scope, and nested functions do not inherit the this value of their caller. [...] Here is how "this" is explicitly defined as a keyword in the JavaScript BNF grammer, in this example as the very first option of PrimaryExpression. Definitely not a variable or Identifier. Quite magic. No other "variable" name is defined as a top level PrimaryExpression. "this" is very special. https://tomcopeland.blogs.com/EcmaScript.html https://tomcopeland.blogs.com/EcmaScript.html PrimaryExpression ::= "this" | ObjectLiteral | ( "(" Expression ")" ) | Identifier | ArrayLiteral | Literal You can even ask devtools for its opinion: >var this = 1; VM2547:1 Uncaught SyntaxError: Unexpected token 'this'
- goto11 7y agoNevertheless, scope wise it behaves like an implicit parameter with lexical scope. It just muddies the issue to talk about dynamic scope.
- DonHopkins 7y agoDude or dudess, "this" absolutely does not behave in any way like it is lexically scoped, in any sense of the term. Reality check: Are we even talking about the same language? I'm talking about JavaScript. Which language are you talking about, C++? If you're not talking about JavaScript, then you replied to the wrong comment. Or are you just trolling and gaslighting, because you were offended by the Trump reference? I'll give you the benefit of the doubt that you're also referring to JavaScript, and that you sincerely believe what you say, because you need to change your beliefs. Please go read any elementary JavaScript tutorial, or any of the thousands of blog posts by people who got screwed by accidentally mis-understanding for a moment and mistakenly thinking that "this" was lexically scoped, then got burnt, but learned from the experience, and wanted to share with everyone else what they learned by shooting themselves with an "oft-fired footgun": "this" is dynamically scoped, and changes meaning at runtime, regardless of the surrounding lexical environment in which it was defined, UNLESS you use fat arrow functions, which were specifically designed to work around that problem. If "this" behaved like an implicit parameter with lexical scope, then why did they bother adding fat arrow functions to the language as an afterthought, to work around the problem you don't seem to believe exists? One more time, I will quote the rationale section of Brendan Eich's fat arrow proposal: https://tc39wiki.calculist.org/es6/arrow-functions/ https://tc39wiki.calculist.org/es6/arrow-functions/ >"dynamic this binding is an oft-fired footgun" -Brendan Eich, ES6 Arrow Functions Proposal If you're right, then Brendan Eich, Douglas Crockford, Anders Hejlsberg, many other expert designers and programmers of JavaScript and TypeScript, and a whole lot of other people, are also extremely confused: https://www.forumone.com/ideas/typescript-application-scale-javascript/ https://www.forumone.com/ideas/typescript-application-scale-... >TypeScript borrows some features from the upcoming ECMAScript 6 specification, too. There is convenience syntax for day-to-day programming tasks, such as the arrow function, borrowed from CoffeeScript. The arrow function, which looks like this: (foo, bar) => baz, is shorthand notation for (function (foo, bar) { return baz; }).bind(this). This automatically captures the current value of “this” in scope, which is more often than not what you want. The proposal even states “dynamic this binding is an oft-fired footgun.” http://jsnext.blogspot.com/2012/07/arrow-functions.html http://jsnext.blogspot.com/2012/07/arrow-functions.html >Arrow Functions are lexically bound to the 'this' of where it is declared. In other words, when an Arrow Function is declared, whatever the scope is at that time, that is 'this'. [...] Why add Arrow Functions? Why add this? We already understood what was going on... so why change it? I think that Brendan Eich has provided a great list of reasons ('Rationale' section): [...] However, we don’t want CoffeeScript’s ->, it’s confusing to have two arrows and dynamic this binding is an oft-fired footgun. What exactly do you believe Brendan Eich meant by "dynamic this binding"? How can "this" be dynamically bound, yet behave "like an implicit parameter with lexical scope" (as you wrongly claim) at the same time? Is Brendan Eich and everyone else in the JavaScript and TypeScript communities mistaken, and they foolishly added fat arrow functions to JavaScript for no reason, but nobody pointed out that embarrassing mistake until you came along? If you're right, then you're barking up the wrong tree by arguing with me about it, so by all means, get on Twitter and tell Brendan Eich himself, ASAP! Your argument is with him and everyone else, not me. Maybe it's not too late to turn that ship around, but in my opinion it's already sailed. But if you're wrong, then you probably have a lot of bugs to fix in your code, and should stay off Twitter. If you truly believe in your heart of hearts that "this" is lexically scoped, you have definitely proven my and Brendan's point that "this" is a terrible "oft-fired footgun" that causes lots of deep confusion and insidious misunderstanding: just take a step back and look at how sure you are of yourself. Because "this" is certainly more mysterious and dangerous than YOU believe it is.