revive let blocks

Kyle Simpson getify at gmail.com
Thu Jun 18 12:30:46 UTC 2015


> Apart from complicating the engine and the grammar

I've tried to figure out what complications it introduces. In my imperfect analysis, it hasn't seemed like much. I've written a transpiler tool[1] that finds `let (x..) { .. }` occurrences and changes them to `{ let x.. .. }`. It was pretty easy to do. I would imagine a similar technique could work for other transpilers and even the engines themselves (simple AST transformation).

I'm sure there are other/obscure issues I'm missing, but the fact that this still works in FF leads me to believe it is a tenable feature at least.

> what advantage does the second version have over the first one?

The primary advantage is that it's an explicit form that syntactically forces the main `let` declarations to the top of the block, eliminating the TDZ hazard (at least for those).

Ofc that's not to say that you can't also do other `let` declarations inside the block and shoot yourself in the foot. But at least for the main ones you're declaring for the block, it's clear and obvious what variables will exist for that block's scope by looking at the top of the block.

That notion is similar to the advantages many devs feel/felt from putting all the `var` declarations at the top of a function declaration, or locating the formal function parameters explicitly in the function declaration instead of implicitly pulling in local variable declarations from `arguments`:

```js
function foo() {
   var [a,b,c] = arguments;
   // ..
}

// vs:

function bar(a,b,c) {
   // ..
}
```
  
> Why do you prefer it to the first one?

I prefer creating explicit blocks for scope rather than implicitly hijacking existing blocks for scope. I prefer to be able to reason about my `if` block separately from a localized block of scope that may appear inside it. That's why I create the explicit `{ .. }` block to put my `let` declarations in. And that's why the next logical step is to move the `let` declarations to a syntactic form that forcibly attaches them to the block, making the purpose/intent of the block all that much clearer.


  [1] https://github.com/getify/let-er



More information about the es-discuss mailing list