简体   繁体   English

JavaScript中的执行上下文和对象

[英]Execution context and object in JavaScript

There's something about object, execution context in JS that I don't understand. 我不了解JS中有关对象,执行上下文的一些信息。

When we create an object, does it create an execution context ? 当我们创建一个对象时,它会创建一个执行上下文吗? since an execution context is created when a function is invoked. 因为执行上下文是在调用函数时创建的。 And if it doesn't, so the object is just like the others variable in the actual execution context ? 如果没有,那么该对象就像实际执行上下文中的other变量一样?

Thank you. 谢谢。

When we create an object, does it create an execution context ? 当我们创建一个对象时,它会创建一个执行上下文吗?

No. 没有。

since an execution context is created when a function is invoked. 因为执行上下文是在调用函数时创建的。

That's true, but creating an object is different from invoking a function. 是的,但是创建对象与调用函数不同。

And if it doesn't, so the object is just like the others variable in the actual execution context ? 如果没有,那么该对象就像实际执行上下文中的other变量一样?

The object exists in memory, and a reference to it exists in any variable or property you stored it in. If you store it in a variable, that variable is held in a lexical environment object associated the execution context where the variable was declared. 该对象存在于内存中,并且对其的引用存在于您将其存储在其中的任何变量或属性中。如果将其存储在变量中,则该变量将保存在与声明该变量的执行上下文相关联的词法环境对象中。

A concrete example might help: 一个具体的例子可能会有所帮助:

function foo() {
    var n = 42;
    var o = {};
    console.log(n, o.toString()); // 42, [object Object]
}
foo();

Calling foo creates an execution context and a lexical environment object associated with it. 调用foo创建一个执行上下文以及与之关联的词法环境对象。 The n and o variables are bindings stored in that lexical environment. no变量是存储在该词汇环境中的绑定 The n binding's value is the primitive number 42. The o binding's value is a reference to the object. n绑定的值是原始数42. o结合的值是对对象的引用 The object itself exists elsewhere in memory. 对象本身存在于内存中的其他位置。

+−−−−−−−−−−−−−−−−−−−−+
| Execution Context  |
+−−−−−−−−−−−−−−−−−−−−+   +−−−−−−−−−−−−−−−−−−−−−+
| Current Lex Env    |−−>| Lexical Environment |
| (some other stuff) |   +−−−−−−−−−−−−−−−−−−−−−+
+−−−−−−−−−−−−−−−−−−−−+   | n: 42               |   +−−−−−−−−+
                         | o                   |−−>| Object |
                         | (some other stuff)  |   +−−−−−−−−+
                         +−−−−−−−−−−−−−−−−−−−−−+

But again, that context and its lexical environment are created by the call to foo , not by creating an object. 但是同样,该上下文及其词法环境是通过对foo的调用而不是通过创建对象来创建的。

Once foo returns, if no closures were created within foo , the execution context and its associated lexical environment are eligible for garbage collection. foo返回后,如果在foo未创建任何闭包,则执行上下文及其关联的词法环境可以进行垃圾回收。

Getting a bit far afield of your question: If we'd created a closure within foo and retained a reference to it even after foo returned, the lexical environment would be retained by that closure; 让您的问题走得更远:如果我们在foo创建了一个闭包,并且即使在foo返回之后也保留了对它的引用,那么该闭包将保留词汇环境; more on closures in this question and its answers . 更多关于这个问题及其答案的闭包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM