Skip to content

Instantly share code, notes, and snippets.

@olov
Created August 22, 2013 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olov/6307928 to your computer and use it in GitHub Desktop.
Save olov/6307928 to your computer and use it in GitHub Desktop.
Lot's of good stuff from Ariya as always and expected! :) I wanted to chime in with a couple of comments regarding ES6 block scope, const in particular. It's kind of a pet-peeve of mine (I'm the author of the defs transpiler).
The article describes Mozilla-JS const, not ES6 const. Mozilla-JS const is not block scoped and is silent on reassignment. ES6 const is block scoped and reassignment is a static error found at parse time (see https://gist.github.com/olov/6255863) before program execution. Do note that you have to opt-in to using ES6 const in v8/node via strict mode, otherwise you'll get Mozilla-const semantics. Prepend a "use strict"; to the example and re-run it - *boom* you're now in the beautiful world of ES6 const. :)
Also, while not an error, my humble opinion is that we're missing out big time by teaching that "the major use case for const is to safeguard important constants". While that may have been true in a Mozilla-JS const world, in ES6 const means that the variable binding won't change - and that this is enforced. You can (and should!) assign (non-constant!) objects, arrays and what have you to ES6 consts. That doesn't mean that they are immutable, but it means that the variable name will always refer to exactly that object/array instance. You then use var only for variables that gets reassigned, like loop iterators, accumulators and such. Used like that, const becomes a very powerful tool both for the code writer and reader to easily understand which of your variables changes and which doesn't. And it's verified every time your program loads, before execution, so you can rest assured that the contract is respected. See "const is the norm" in http://blog.lassus.se/2013/05/defsjs.html for a more thorough description. It's up to the community to decide how they will use ES6 const at the end of the day, let's teach the full options!
Cheers, /Olov
@ariya
Copy link

ariya commented Aug 28, 2013

I'll try to inject the addendum on the early error of const reassignment (I'm swamped right now, it's a long story but if I could have done it earlier, I would).

For the second point, I don't disagree with you. I do however think that there are multiple different ways to "teach" web developers all the good things. I have experimented with bite-sized teasers and found that model works for my purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment