Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
嵌套JavaScript函数_Javascript - Fatal编程技术网

嵌套JavaScript函数

嵌套JavaScript函数,javascript,Javascript,我想制作嵌套JavaScript函数作为概念证明。我找到了一个例子,并对其进行了一些修改,以符合我的观点: var t = { nestedOne: { nest: function() { alert('nest'); this.nestedTwo.nest2(); }, nest3: function() { alert('nest3'); },

我想制作嵌套JavaScript函数作为概念证明。我找到了一个例子,并对其进行了一些修改,以符合我的观点:

var t = {
    nestedOne: {
        nest: function() {
            alert('nest');
            this.nestedTwo.nest2();
        },
        nest3: function() {
            alert('nest3');
        },       
        nestedTwo: {
           nest2: function() {
              alert('nest2');
              t.nestedOne.nest3();
           }       
        }
    }
};

t.nestedOne.nest();

// *** Output is nest, nest2 and nest3 ***

这是可行的,但我想知道为什么在nest2中,我必须通过
t.nestedOne.nest3
调用,而不是
This.nestedOne.nest3
,这与我从nest2调用函数的方式类似。

函数调用的上下文由调用函数的对象决定,而不是路径中最左边的对象

这:

不是这个:

this.nestedTwo.nest2();
^^^^

这一切都是关于
这个

最简单的解释方法是对代码稍作修改:

var t = {
    nestedOne: {
        nest: function() {
            console.log('nest',this);
            this.nestedTwo.nest2();
        },
        nest3: function() {
            console.log('nest3',this);
        },       
        nestedTwo: {
           nest2: function() {
              console.log('nest2',this);
              t.nestedOne.nest3();
           }       
        }
    }
};

t.nestedOne.nest();
上面的输出是

嵌套对象{nestedTwo={…},nest=function(),nest3=function()}
nest2对象{nest2=function()}
nest3对象{nestedTwo={…},nest=function(),nest3=function()}

请注意,在第二个调用中,
指的是函数,而不是对象

现在,您可以进行以下两个更改

  • 调用
    next2
    this
    的上下文中传递:
    this.nestedTwo.nest2.call(this)

  • nest2
    中使用
    this
    this.nest3()

以及所有预期工程:

var t={
内斯特敦:{
嵌套:函数(){
log('nest',this);
this.nestedTwo.nest2.调用(this);
},
nest3:function(){
console.log('nest3',this);
},       
嵌套的两个:{
nest2:function(){
console.log('nest2',this);
这是nest3();
}       
}
}
};

t、 nestedOne.nest()很多都不是嵌套函数,而是嵌套对象。出于同样的原因,你不能在你的
nest
方法中使用
this.nestedOne.nestedTwo.nest2()
。嗯,当我单击“运行代码片段”时,我在输出窗口中看不到任何东西。奇怪,我知道-你在哪个浏览器上?
var t = {
    nestedOne: {
        nest: function() {
            console.log('nest',this);
            this.nestedTwo.nest2();
        },
        nest3: function() {
            console.log('nest3',this);
        },       
        nestedTwo: {
           nest2: function() {
              console.log('nest2',this);
              t.nestedOne.nest3();
           }       
        }
    }
};

t.nestedOne.nest();