@shading

JavaScript контекст?

"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()

Почему везде this.x === undefined?
  • Вопрос задан
  • 106 просмотров
Решения вопроса 1
xmoonlight
@xmoonlight
https://sitecoder.blogspot.com
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 blocks they are defined in. The difference between let and const is that a const 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.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы