3 ms·
It always struck me about JavaScript, that if you were going to choose one variable and one variable only to be dynamically scoped (for whatever strange reason)
by DonHopkins 7y ago
It always struck me about JavaScript, that if you were going to choose one variable and one variable only to be dynamically scoped (for whatever strange reason), "this" would be the absolute worst one to choose that you possibly could, an automatic nuclear foot-gun that would trip you up every time. If you ever wanted a function to lexically close over a variable, but could choose only one, "this" would be it!
Fat arrow functions struck me like one of those half-assed solutions Trump (edit: or Obama) comes up with for a terrible problem of his own creation, a foolish mistake that nobody in their right mind would have ever made in the first place. And then he expects praise for his solution, and nobody remembers he caused the problem it was supposed to solve in the first place.
I agree with the sibling comment by austincheney:
JavaScript would have been a much simpler and less bug-prone language if not for "this" being dynamically scoped and magical, which later required nailing fat arrow functions onto the side, that were almost but not quite entirely unlike functions.
Have you ever tried explaining why there are two different kinds of functions in JavaScript to a beginning programmer, and seen their eyes glaze over when you digressed into the nuances of "this", lexical scoping, dynamic scoping, call and apply, closures, and binding functions? You have to understand all that before you truly understand functions in JavaScript, and much of it is irrelevant crap.
Lexical, dynamic, both, or neither? Pick a lane, JavaScript!
- joyfulmantis 7y ago"Eschew flamebait. Don't introduce flamewar topics unless you have something genuinely new to say. Avoid unrelated controversies and generic tangents." "Please don't use Hacker News for political or ideological battle. That destroys the curiosity this site exists for."
- DonHopkins 7y agoWhat, we can't talk about JavaScript?! At least I didn't mention Brendan Eich's deep and tragic mistaken misunderstanding of equality. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- joyfulmantis 7y agoI was not referring to what you said about JavaScript, but rather your inclusion of a unrelated, unnecessary and highly polarizing political tangent.
- DonHopkins 7y agoI'm sorry, I didn't think it was controversial that Trump did that kind of stuff, and it made a perfect metaphor. But even if you disagree about Trump, I think you can still understand the metaphor about JavaScript fat arrow functions. Can you think of a better metaphor? Edit: I added "(or Obama)" for people who are offended by me pointing out what everyone knows about Trump.
- at_a_remove 7y agoMany of us are trying to escape "Trump Derangement Syndrome" which has become infectious almost everywhere else.
- saagarjha 7y ago> Can you think of a better metaphor? One that doesn’t involve politics?
- DonHopkins 7y agoProgramming language design IS politics. Especially JavaScript. How do you think it got its name?
- saagarjha 7y agoNot that kind of politics.
- minitech 7y agoDo you mean you wouldn’t have named it “this”? Because if `this` weren’t set by the means of calling in ES5 and earlier, it would have no point at all.
- DonHopkins 7y agoIt doesn't matter what it's named. It matters how it's passed. Python, Common Lisp CLOS, ScriptX, even NeWS's object oriented PostScript, all pass "self" as a first explicit parameter. C++ does it behind the scenes, but it's just a hidden parameter, not a property of the hidden "virtual machine", because C++ doesn't have a virtual machine. But if you look at the C code generated by the CFront C++=>C compiler, you will see that every method has an explicit first "this" parameter whose type is the class it's defined in. The way "this" is defined in JavaScript unnecessarily exposes the internal implementation details of the first JavaScript interpreter, which all subsequent JavaScript interpreters and compilers then had to be compatible with. Simply knowing about normal function parameters and lexical binding like in Python is not enough: There is an invisible a VM interpreter thread somewhere that you have to know about, which has a "this" property (or "horrible global object", as Douglas Crockford puts it), and the only way to change it is indirectly through method applications with ".", or by using call and apply. So JavaScript "this" is a magic keyword, not just a normal parameter like all the rest. In the same way JavaScript "arguments" is magic, not just a normal argument like Python or Lisp &rest arguments. And this needlessly adds to the complexity and surface area of the language, and bakes in unwanted behaviors like dynamic binding, when most of the civilized world agrees that lexical binding is superior, less surprising, and easier to reason about, and dynamic binding should be the exception that you have to go out of your way to use, not the default behavior for the implicit first argument of every method or function.
- minitech 7y ago> Simply knowing about normal function parameters and lexical binding like in Python is not enough: There is an invisible a VM interpreter thread somewhere that you have to know about, which has a "this" property, and the only way to change it is indirectly through method applications with ".", or by using call and apply. So "this" is not just a normal parameter like all the rest. That sounds like an unnecessarily complicated and possibly wrong way to think about it. `this` acts exactly like a parameter in ES5 strict mode (`x.f()` being `f.call(x)` and `f()` being `f.call(undefined)`), and does not act like dynamic binding.
- pwdisswordfish2 7y ago‘this’ is not in any sense dynamically scoped. It's just a function parameter, assigned implicitly based on the context of the call.
- DonHopkins 7y agoIf that's a fact, then please tweet to Brendan Eich that he was wrong when he referred to "dynamic this binding" as the rationale for adding fat arrow functions to JavaScript in his fat arrow proposal, and take your argument up with him, not me: >"dynamic this binding is an oft-fired footgun" -Brendan Eich https://tc39wiki.calculist.org/es6/arrow-functions/ https://tc39wiki.calculist.org/es6/arrow-functions/
- BrendanEich 7y agoI wrote dynamic binding, that's not the same as dynamic scope.