5 ms·
There are a couple of things that JavaScript got so incredibly correct: * Lexical scope natively without syntax or conventions * Functions as first class citi
by austincheney 5y ago
There are a couple of things that JavaScript got so incredibly correct:
* Lexical scope natively without syntax or conventions
* Functions as first class citizens
* Inheritance, classes, OOP are not forced on you and can be ignored/omitted entirely from your code
Those few things are what has kept me hooked on the language.
> Node is not ECMAScript in a server-side ECMAScript engine, it's JavaScript in a browser JavaScript engine.
What does that even mean? The APIs are completely unrelated and Node does different things that don’t make sense in the browser.
- galangalalgol 5y agoThat last bullet is my main complaint with java and c#.
- wvenable 5y agoC# just removed the requirement to start code with a class or a main method; you can just start typing into the file.
- metaltyphoon 5y agoI like this so much. In fact they are investigating the idea of #!/usr/bin/dotnet on a .cs file!
- titzer 5y ago> * Lexical scope natively without syntax or conventions I assume you mean over the alternative, dynamic scoping like in e-Lisp. But you realize that JavaScript's scoping rules are entirely wonky, e.g. that "var" declarations are lifted to the function level?
- austincheney 5y agoTo be more specific the term is hoist which means the declarations are pushed to the top of the scope at run time for maximum availability, and var is function scoped instead of block scoped. Block scoping is often more specific because a function body is a block as well.
- errantspark 5y agoRight but we have `let` now which solves that problem.
- titzer 5y agoNot completely, `let` also has a weird temporal dead zone[1], which is basically a dynamic check. [1] https://www.freecodecamp.org/news/what-is-the-temporal-dead-zone/ https://www.freecodecamp.org/news/what-is-the-temporal-dead-...
- IshKebab 5y agoThat's a good thing, and I'm not sure what you mean about it being a dynamic check.
- titzer 5y agoIt's a dynamic error if you access a let-bound variable before it is actually initialized. In most other languages that have lexically-scoped declarations, it simply would not be visible. So I think JS's choice is actually a regression.
- hwillis 5y agovar is a terrible example of that. Just don't use it. A good example would be the scoping in for loops and maybe arrow functions. For loops are goofy but virtually never an issue, and arrow functions are awesome.
- deleted 5y ago[deleted]
- cush 5y agoThey were referring to `let`
- hollerith 5y ago>a couple of things that JavaScript got so incredibly correct: . . . Lexical scope natively without syntax or conventions What language of the last 28 years doesn't "natively" lexically scope its variables?
- z3t4 5y agoJavaScript is different in that it use the function as the lexical scope rather then the angel brackets. This forces you to think about the code as functions, rather then blocks. And in turn helps you think of the code in three dimensions where time is one dimension, making it easier to understand code that runs asynchronously.
- z3t4 5y agoBecause people coming from other languages find this confusing the let (and const) keyword was specified (ES6/2015) for block/angel-bracket lexical scope. Whereas var is function scoped.
- icedchai 5y agoNot Javascript. If I forget a var or let, it's global.
- austincheney 5y agoStrict mode throws an error on implied globals. Use of modules forces strict mode.
- dclowd9901 5y agoAnd here I thought scope was kind of disaster in JS: - `const fooFunc = () => {}` is treated differently than `function fooFunc() {}` in declaration and subsequent access. - A function's invocation context changes depending on how it was declared (fat arrow vs. function declaration) Don't get me wrong: I love JS, but this is not one of the things I felt made it intuitive.
- austincheney 5y agoThe this context changes, but I recommend developers not use this. That isn’t a scope issue though.
- mweberxyz 5y agoThere would be no point in having two ways to do the exact same thing if function-style declaration and arrow-style declaration implementations were identical. Why would they add a new feature to the language if it was purely syntactic sugar?
- senthil_rajasek 5y ago>> Node is not ECMAScript in a server-side ECMAScript engine, it's JavaScript in a browser JavaScript engine. >What does that even mean? The APIs are completely unrelated and Node does different things that don’t make sense in the browser. I have taken the privilege of rewriting the quoted part of the parent comment, Node runs javascript on the server side in an engine built for the browser. It does not run ECMAScript with an engine specially made for server side apps.
- wernercd 5y agoAnd those things are outweighed by things JavaScript got so incredibly wrong... like === vs == and some of the weirdness when you GASP use == as any other language would do. JS is like PHP... only a popular language because it's been around a long time and it's easy to get started. Otherwise, there are SOOO many better programming language out that the ONLY reason JS still has traction is it became the "default" web language.
- IshKebab 5y agoThis comment is so weird. JavaScript got scoping completely wrong, and none of those were particularly novel when it was designed and they are almost universal now so I don't know why they would keep you hooked on JavaScript! Maybe try some other languages...