Skip to content

Instantly share code, notes, and snippets.

@jorendorff
Created July 30, 2015 19:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jorendorff/a15e7aeb52c87c88fc2e to your computer and use it in GitHub Desktop.
Save jorendorff/a15e7aeb52c87c88fc2e to your computer and use it in GitHub Desktop.
  • the problem: hoisting

    • closures in loops: sadface

    • is this worth solving?

      • the closures in loops thing is pretty bad; ideally we want things to work the first time

      • but still, maybe not

  • the problem: global names in modules (or is it a problem?)

  • the solution: let is the new var

    • how does let differ from var?

      • let is block-scoped

      • global let is not a property on the global object

      • for(let) is a new binding each time through

      • even for(let;;)

      • the temporal dead zone

        • try to present this as a convenient "oh you have a bug" kind of error
        • so let-declared variables are still hoisted, but only to the start of the enclosing block. and it's an error to try and access them before the declaration is reached.
        • this is a weird compromise design and I wouldn't have gone this route myself.
        • (some languages scope each variable to the code following the declaration, to make this a compile-time error, but because functions are hoisted in JS, the design of let was constrained.)
      • the global lexical tier

      • at toplevel in modules, var acts like let.

    • let is a reserved word in strict mode code. for backward compatibility, you can still declare a variable named let in non-strict code, but please don't do this

    • bindings introduced by catch follows similar rules--- catch has been block-scoped all along.

    • class bindings are similar.

  • const

    • just like let, but you can't assign to it (that's a SyntaxError)
    • only initialized at the point in your code where it's declared
    • TDZ error to refer to it before that point
  • implemented?

    • in firefox by Brendan; redesigned drastically in committee; updated by Shu-yu Guo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment