从逻辑的角度来看,JavaScript执行上下文和JavaScript对象是一回事吗? [英] Are JavaScript execution contexts and JavaScript objects the same thing from a logical point of view?

查看:51
本文介绍了从逻辑的角度来看,JavaScript执行上下文和JavaScript对象是一回事吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript的理解是,在脚本执行时会创建一个全局执行上下文 - 我理解为保留内存空间中的一系列键值:值对 - 非常类似于常规JavaScript对象。

My understanding of JavaScript is that on script execution a global execution context is created - what I understand to be a series of key:value pairs in reserved memory space - much like a regular JavaScript object.

在函数执行时,创建一个新的执行上下文,可以访问父执行上下文。这似乎(对我而言)至少相当于重新初始化JavaScript执行环境。除了最初在全局对象中设置的键:值对之外,需要将其添加到新的执行上下文,因为子执行上下文可以访问父键的值:值对。

On function execution, a new execution context is created with access to the 'parent' execution context. This seems (to me) at least to be the equivalent of re-initializing the JavaScript execution environment. Except that non of the key:value pairs initially set up in the global object need to be added to the new execution context since the child execution context has access to the parent's key:values pairs.

例如:

function Y() {
   this.prop = 4;
};

Y.prototype.doY = function() {
  console.log(this.prop);
};

function X(){
  this.prop = 5;
  this.y = Object.create(Y.prototype);
  Y.call(this.y);
};

// I can instantiate the object like so:
var x = new X();

// Which is equivalent to the following:
var x = Object.create(X.prototype);
X.call(x);

// Then I can add a function to the object that the X constructor points to via the X.prototype reference:
X.prototype.doX = function() {
  console.log(this.prop)
};

// And then i can execute doX() in the context of the x object
x.doX(); // 5
x.y.doY(); // 4

执行时, doX 功能创建从 x 对象中引用的执行上下文。反过来, x 对象在全局对象中引用。

On execution, doX function creates an execution context referenced from within the x object. The x object in turn is referenced within the global object.

同样,在执行时 doY 函数创建一个从 xy 对象中引用的执行上下文。

Likewise, on execution the doY function creates an execution context that is referenced from within the x.y object.

在我看来,从逻辑的角度来看,由函数执行创建的执行上下文基本上等同于JavaScript对象:

It seems to me that an execution context as created by function execution is basically equivalent to a JavaScript object from a logical point of view:


  • 两者都允许变量声明对父对象/执行上下文不可见

  • 两者都包含对父对象/执行上下文的某种内部引用

似乎对象和执行上下文只是一个key:值列表,整个列表由一个(只有一个)父对象引用(即 y 对象作为 x 执行上下文中的引用存在 - 或者作为根父对象的全局对象( x 对象作为t中的引用存在全局执行上下文)。

It seems that both objects and execution context are just a list of key:values, the whole list being referenced by a single (ONLY a single) parent object (i.e. the y object exists as a reference within the x execution context) - or the global object as the root parent object (the x object exists as a reference within the global execution context).

所以问题是:JavaScript执行上下文是否与JavaScript对象相同?如果没有,有什么区别?

So the question is: Is JavaScript execution context the same as a JavaScript object? If not, what is the difference?

这个问题:执行上下文和变量对象在JavaScript中实际上是一回事吗?提到在实现方面,执行上下文和对象不一样事情。

This question: Are Execution Context and Variable Object actually same thing in JavaScript? mentions that in terms of implementation, that an execution context and an object are NOT the same thing.

说执行上下文是否继承自Object是否正确?或者是对象/执行上下文的实现是完全不相关的...我可以看到的一个区别是,在创建时,执行上下文从上到下(词汇上)被处理,允许作者指定命令性命令 - 即指示计算机做事。但就建筑而言,这种差异似乎是对象概念的延伸,而不是完全不同的东西。

Would it be correct to say that execution context inherits from Object? Or is implementation of objects/execution contexts completely non-related... One difference that I can see is that on creation, an execution context is 'processed' from top to bottom (lexically speaking) allowing an author to specify imperative commands - i.e. instruct the computer to do things. But in terms of architecture, this difference seems like an extension of the idea of an object rather than something completely different.

在我看来,如果存在这样的运行JavaScript环境,基本上就是一棵树。根节点是全局执行上下文,创建对象将节点添加到根节点,或者(如上面的y的情况)将节点添加到子节点。然后,子节点通过属性引用父节点,以允许对父执行上下文进行作用。

It seems to me that a 'running JavaScript environment' if such a thing exists, is basically a tree. The root node is the global execution context, and creating object adds nodes to the root node, or (as in the case of y above), adds nodes to the child nodes. Child nodes then reference parent nodes via a property to allow for scoping to parent execution context.

然后就闭包而言,涉及执行上下文的创建(和执行) ,返回时,结果对象看起来非常像常规对象,因为闭包引用的执行上下文将永远不会再次执行(即,相同的函数,重新执行将创建新的执行上下文和闭包)。

Then in terms of closures, which involve creation (and execution) of an execution context, on return the resultant object seems EXACTLY like a regular object since the 'execution context' as referenced by the closure will never again be executed (i.e. the same function, re-executed would create a new execution context and closure).

所以从应用的角度来看,有没有时间这个 - >即自我引用当前执行上下文与进行函数调用的对象不同(除了使用调用 apply ,或 bind )?即在 xydoY()的情况下,从 xy调用函数 doY object。

So from an "applied" point of view, is there ever a time that this -> i.e. a self reference to the current execution context, is NOT the same as the object from which a function call is made (aside from when using call, apply, or bind)? i.e. in the case of x.y.doY(), the function doY is called from the x.y object.

推荐答案

执行上下文概念对象,是的,与规范中的其他几个一样。这并不能使它们与JavaScript对象相同。它们是规范设备,可能根本不存在于运行时;代码不能直接引用它们。有关完整图片,请阅读可执行代码和执行上下文在规范中。

Execution contexts are conceptually objects, yes, as are several other things in the specification. That doesn't make them the "same" as JavaScript objects. They're a specification device and may not literally exist at runtime at all; code cannot directly reference them. For the full picture, read Executable Code and Execution Contexts in the spec.


JavaScript执行上下文是否与JavaScript对象相同?

Is JavaScript execution context the same as a JavaScript object?

不。 JavaScript对象可以直接由代码引用,具有原型,并且具有针对它描述的特定行为,这些行为没有针对执行上下文等描述。

No. A JavaScript object can be directly referenced by code, has a prototype, and has specific behaviors described for it which are not described for execution contexts, etc.


说执行上下文是否继承自Object是否正确?

Would it be correct to say that execution context inherits from Object?

不,规范中没有任何内容表明是case。

No, nothing in the spec suggests that to be the case.


在我看来,如果存在这样的运行JavaScript环境,基本上就是一棵树了。

It seems to me that a 'running JavaScript environment' if such a thing exists, is basically a tree.

相当间接的排序。规范没有描述从执行上下文到其父或子上下文的任何链接。相反,词汇环境对象是上下文状态的一部分(具体而言) LexicalEnvironment和VariableEnvironment状态组件)具有指向其父词法环境的链接,以便进行绑定解析。 (父lexenv在规范中没有指向其子节点的链接。)但它们也纯粹是一个规范设备,它可能在运行时不存在,也不能被代码直接引用。

Sort of, fairly indirectly. The spec doesn't describe any link from an execution context to its parent or child contexts. Instead, the lexical environment objects which are part of the context's state (specifically the LexicalEnvironment and VariableEnvironment state components) have a link to their parent lexical environment for the purposes of binding resolution. (The parent lexenv doesn't have a link to its children in the spec.) But they, too, are purely a specification device which may not literally exist at runtime and cannot be directly referenced by code.

因此将它们视为对象是绝对正确的,这就是规范描述它们的方式,但它们不是通常理解术语的方式的JavaScript对象 —代码可以直接引用和操作。

So it's absolutely correct to think of them as objects, that's how the spec describes them, but they aren't JavaScript objects in the way that term is normally understood — something code can directly reference and act upon.

这篇关于从逻辑的角度来看,JavaScript执行上下文和JavaScript对象是一回事吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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