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://43.154.161.224:23101/article/api/json?id=7116&siteId=1