简体   繁体   English

执行上下文在简单示例中如何显示

[英]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.

相关问题 如何使ExtJs应用程序看起来像Feed示例? - How to get an ExtJs application to look like the Feed Example? 根据 url 示例,axios 的参数应该如何 - How should the parameters for axios look like according to the url example 理解经典执行上下文示例 - Understanding a classical execution context example Javascript中的一个简单的上下文更改示例 - A simple this context change example in Javascript jQuery拖放示例:排序列表在目的地看起来不像排序列表 - jQuery Drag/Drop Example: Ordered List does not look like ordered list at the destination 在HTML5规范中,似乎支持嵌套脚本标记,那么真实示例是什么样的呢? - In HTML5 Spec, it seems the nested script tags are supported, so what does a real example look like? 为什么在本例中原型函数的执行上下文(“ this”)是错误的? - Why the execution context (“this”) of prototypical function is wrong in this example? 如果... else格式,此JS代码如何显示? - How does this JS code look like in if…else format? javascript执行上下文在具有多个窗口的firefox扩展中如何工作? - how does javascript execution context work in firefox extensions with multiple windows? 为什么在此示例中“ this”的上下文发生变化? - Why does the context of “this” change in this example?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM