"use strict"
let x = 1
function Bizz() {
x = 2
return {
hazz: function () {
let x = 3
return {
haff: function () {
let x = 4
console.log(this.x)
}
}
},
haww: function () {
let x = 5
setTimeout(() => {
console.log(this.x)
})
},
}
}
Bizz().hazz().haff()
Bizz().hazz()
Bizz().haww()
ECMAScript 6 (ES6/ES2015) introduced thelet
andconst
keywords that support the declaration of block scope local variables. This means the variable will be confined to the scope of a block that it is defined in, such as anif
statement orfor
loop and will not be accessible outside of the opening and closing curly braces of the block. This is contrary tovar
declarations which are accessible outside blocks they are defined in. The difference betweenlet
andconst
is that aconst
declaration is, as the name implies, constant - a read-only reference to a value. This does not mean the value is immutable, just that the variable identifier cannot be reassigned.