JavaScript: implement let-working with var

Spawni :

So I have a code that clone let-behavior. But I don't undestand how and why it is working. Can someone explain it?

(function() {
  var a = 2;
})()
Quentin :

let is scoped to the block it appears in.

var is scoped to the function it appears in.

By replacing a block with a function (which is immediately invoked) var is scoped to the same lines of code as let would be.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=7116&siteId=1