JavaScript context?

"use strict"

let x = 1
Bizz function() {
 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()

Why all this.x === undefined?
April 4th 20 at 13:15
1 answer
April 4th 20 at 13:17
Solution
ECMAScript 6 (ES6/ES2015) introduced the let and const 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 an if statement or for loop and will not be accessible outside of the opening and closing curly braces of the block. This is contrary to var declarations which are accessible outside the blocks they are defined in. The difference between let and const is that a const declaration is, as the name implies, the constant is a read-only reference to a value. This does not mean the value is immutable, just that the variable identifier cannot be reassigned.

Find more questions by tags JavaScript