【问题标题】:Object's context ('this') between nested functions at a class [duplicate]类中嵌套函数之间的对象上下文('this')[重复]
【发布时间】:2021-08-07 04:18:58
【问题描述】:

希望你们都做得很好。

我有一个关于对象的上下文如何在 Node.JS(或一般的 JS)上工作的问题。 我明白"when you invoke a top-level function in Javascript, the this keyword inside the function refers to the default object"。例如,我有以下功能:

function nestedFunctions() {
    var a = "I am a variable"
    console.log("Initial context:")
    console.log(this)
 
    function one() {
        console.log("First nested function, this is my this context:")
        console.log(this)
        console.log("First nested function, this is my a value:")
        console.log(a)
 
        function two() {
            console.log("Second nested function, this is my this context:")
            console.log(this)
            console.log("Second nested function, this is my a value:")
            console.log(a)
        }
        two()
    }
    one()
}
nestedFunctions()

给出以下输出:

Initial context:
Object Global
First nested function, this is my this context:
Object Global
First nested function, this is my a value:
I am a variable
Second nested function, this is my this context:
Object Global
Second nested function, this is my a value:
I am a variable

到目前为止,一切正常,函数获取了全局上下文。当我尝试时:

var rex = {
    nestedFunctions() {
        var a = "I am a variable"
        console.log("Initial context:")
        console.log(this)
 
 
        function one() {
            console.log("First nested function, this is my this context:")
            console.log(this)
            console.log("First nested function, this is my a value:")
            console.log(a)
 
            function two() {
                console.log("Second nested function, this is my this context:")
                console.log(this)
                console.log("Second nested function, this is my a value:")
                console.log(a)
            }
            two()
        }
        one()
    }
}
 
rex.nestedFunctions()

输出是:

Initial context:
{ nestedFunctions: [Function: nestedFunctions] }
First nested function, this is my this context:
Object Global
First nested function, this is my a value:
I am a variable
Second nested function, this is my this context:
Object Global
Second nested function, this is my a value:
I am a variable

到目前为止一切都很好。父函数获取 "this" = {​​ nestedFunctions: [Function: nestedFunctions] } 因为是对象上下文,嵌套函数获取全局上下文是因为它们没有绑定到父上下文。

但我怀疑的是下一个案例:

class Dog {
    constructor(name) {
        this.name = name
    }
 
    bark() {
        console.log(`Woof, my name is ${this.name}`);
    }
    nestedFunctions() {
        var a = "I am a variable"
        console.log("Initial context:")
        console.log(this)
 
 
        function one() {
            console.log("First nested function, this is my this context:")
            console.log(this)
            console.log("First nested function, this is my a value:")
            console.log(a)
 
            function two() {
                console.log("Second nested function, this is my this context:")
                console.log(this)
                console.log("Second nested function, this is my a value:")
                console.log(a)
            }
            two()
        }
        one()
    }
}
 
let rex = new Dog('Rex')
rex.nestedFunctions()

输出在哪里:

Initial context:
Dog { name: 'Rex' }
First nested function, this is my this context:
undefined
First nested function, this is my a value:
I am a variable
Second nested function, this is my this context:
undefined
Second nested function, this is my a value:
I am a variable

为什么嵌套函数上的“this”上下文在这种情况下是“未定义”而不是全局上下文,就像前面的示例一样?为什么在处理类时默认上下文是未定义的(而不是“全局”)?

【问题讨论】:

    标签: javascript node.js


    【解决方案1】:

    这是因为 JavaScript class 的主体在 strict mode 中执行。顾名思义,这种模式限制性更强。在这种执行模式的特点中,this 不会在作为裸函数调用的函数中引用window,而是返回undefined

    【讨论】:

    • 这很有意义。所有这些都与在严格模式下执行类的事实相结合。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 2017-02-07
    相关资源
    最近更新 更多