with + let

# Francisco Tolmasky (9 years ago)

Just curious how the following code should behave:

var a = { x: 10 };
with (a)
{
   let x = 20;
}

console.log(a.x);

a.x is still 10 in a repl I tried. However, if it was var x in the with x would be 20. Is let not affecting the with expected behavior?

# Bradley Meck (9 years ago)

var is hoisted and unified for multiple definitions (each definition refers to 1 hoisted variable slot). let cannot have multiple definitions that are in the same lexical scope. Using var inside the loop would have no effect since it is unified to the declaration outside of the loop.

# Michael Ficarra (9 years ago)

Francisco, remember that the with-statement can have any statement as its body. For example, with (a) x = 20; would modify a.x. But the let x in the block is shadowing the x that is in scope in the with-statement body.

Michael Ficarra