[英]How does the execution context look like in simple example
thank you for helping me out. 谢谢你帮我
In a class I am taking I have come across the following code: 在上一堂课时,我遇到了以下代码:
var carlike = function(obj, loc){
obj.loc = loc;
obj.move = move;
return obj;
};
var move = function(){
this.loc++;
};
var ben = carlike({}, 9);
It is my understanding that when the function carlike
is invoked on the last line during the creation phase of its execution context the variable move
is set to undefined
. 据我了解,当在其执行上下文的创建阶段的最后一行调用
carlike
函数时,变量move
设置为undefined
。 So my question is: at which point is move
set equal to function() {this.loc++;};
所以我的问题是:在哪一点
move
设置等于function() {this.loc++;};
? ? I find this confusing since the move function is defined after the carlike function.
我发现这令人困惑,因为move函数是在carlike函数之后定义的。 So how does
carlike
have access to the environment after it. 那么,如何
carlike
访问后的环境。
I really would like to know how this works under the hood in terms of the execution context, scope etc. 我真的很想知道它在执行上下文,范围等方面如何工作。
some things I have read to try and understand this: 我读过一些东西,试图理解这一点:
http://dmitrysoshnikov.com/ecmascript/chapter-1-execution-contexts/ http://corporate.tuenti.com/en/dev/blog/functions-and-execution-contexts-in-javascript-2 http://dmitrysoshnikov.com/ecmascript/chapter-1-execution-contexts/ http://corporate.tuenti.com/en/dev/blog/functions-and-execution-contexts-in-javascript-2
It has access to that move variable because although the function move is declared after the function carlike, the function carlike does not reference move until after move has already been declared, since carlike is called after move is defined as a function. 它之所以可以访问该move变量,是因为尽管在函数carlike之后声明了move函数,但是直到声明了move之后,函数carlike才引用move,因为在move被定义为一个函数之后调用了carlike。 If you make the call to carlike above the definition of move, then move will be set to undefined, since that function does not exist yet.
如果在move的定义上方调用carlike,则move将设置为undefined,因为该功能尚不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.