JavaScript内部函数范围链? [英] JavaScript inner function scope chain?

查看:107
本文介绍了JavaScript内部函数范围链?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例中

var a = 1;
( function(x) {   
   function inner() {
      alert(a);
      alert(x);
      alert(y); 
   }
   var y = 3;
   inner();
})(2);

什么时候函数内部被创建?在外部匿名函数的执行时间或解析时间?

When does function inner get created? during execution time or parsing time of outer anonymous function?

函数内部的范围链是什么?

函数内部的执行上下文和范围链之间有什么区别?

What is the difference between the execution context and scope chain of function inner?

感谢您的启发提前!

推荐答案


什么时候创建函数内部?在执行时间或外部匿名函数的解析时间?

When does function inner get created? during execution time or parsing time of outer anonymous function?

每次执行外部函数时都会创建一个。

One is created each time the outer function is executed.


函数inner的范围链中有什么?

What is in the scope chain of function inner?

当你执行它时,该执行获得一个变量对象(从技术上讲,规范称之为变量环境的绑定对象);由为创建 inner 的外部函数调用创建的变量对象支持;这是由全局变量对象支持的。所以:

When you execute it, that execution gets a variable object (technically the spec calls this the "binding object of the variable environment"); that's backed by the variable object created for the outer function call that created inner; that's backed by the global variable object. So:

+------------------------------+
| global variable obj          |
+------------------------------+
    ^
    |
   +-----------------------------+
   | variable obj for outer call |
   +-----------------------------+
       ^
       |
      +-----------------------------+
      | variable obj for inner call |
      +-----------------------------+




函数inner的执行上下文是什么?

What is in the execution context of function inner?

每个函数调用获得自己的执行上下文。我不太清楚我明白在这里问的是什么。

Every function call gets its own execution context. I'm not quite sure I understand what's being asked here.

你可以阅读所有这些内容(如果你愿意趟过这个问题)散文)在规范的第10节中,特别是第10.4.3节输入功能代码

You can read up on all of this stuff (if you're willing to wade through the treacle of turgid prose) in Section 10 of the spec, and in particular section 10.4.3: "Entering Function Code".

这篇关于JavaScript内部函数范围链?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆