相关疑难解决方法(0)

我对JavaScript中的关键字'this'感到困惑

这是一个例子:

function one() {

    var a = 1;
    two();

    function two() {

        var b = 2;
        three();

        function three() {

            var c = 3;
            alert(a + b + c); // 6

        }
    }   
}

one()?; //calling the function
Run Code Online (Sandbox Code Playgroud)

现在当我们调用函数one()时,结果是6.

所以关于范围链,所有变量都已解决,现在我有一个问题.

当所有变量通过范围链解析时,为什么我们需要这个" this "关键字?

所以如果我们有以下功能:

function a() {
    var a = 'function a';

    function b() {
        var b = 'function b';
        alert (a); //will be function a, without keyword this 
        alert (this.a); // what will be the effect of …
Run Code Online (Sandbox Code Playgroud)

javascript scope this

5
推荐指数
1
解决办法
157
查看次数

标签 统计

javascript ×1

scope ×1

this ×1