开发者

Is there a difference between the terms Execution Context and Scope?

开发者 https://www.devze.com 2023-04-06 02:15 出处:网络
Just wondering if they are the开发者_如何学编程 same thing since some programmers say \"scope\" while others say \"execution context\".Short answer: an execution context is defined by the spec to cons

Just wondering if they are the开发者_如何学编程 same thing since some programmers say "scope" while others say "execution context".


Short answer: an execution context is defined by the spec to consist of 3 components:

  • LexicalEnvironment
  • VariableEnvironment
  • ThisBinding

In general the term "variable scope" is used as a synonym for the LexicalEnvironment component. So, technically speaking, yes, there is a difference. In practice, however, terminology usage varies wildly and does not necessarily line up with the spec.


Longer answer: To the ECMA-262-Spec-cave!

10.3 Execution Contexts

When control is transferred to ECMAScript executable code, control is entering an execution context. Active execution contexts logically form a stack. The top execution context on this logical stack is the running execution context. A new execution context is created whenever control is transferred from the executable code associated with the currently running execution context to executable code that is not associated with that execution context. The newly created execution context is pushed onto the stack and becomes the running execution context.

An execution context contains whatever state is necessary to track the execution progress of its associated code.

...

The LexicalEnvironment and VariableEnvironment components of an execution context are always Lexical Environments. When an execution context is created its LexicalEnvironment and VariableEnvironment components initially have the same value. The value of the VariableEnvironment component never changes while the value of the LexicalEnvironment component may change during execution of code within an execution context.

In most situations only the running execution context (the top of the execution context stack) is directly manipulated by algorithms within this specification. Hence when the terms "LexicalEnvironment", "VariableEnvironment" and "ThisBinding" are used without qualification they are in reference to those components of the running execution context.

...

10.3.1 Identifier Resolution

Identifier resolution is the process of determining the binding of an Identifier using the LexicalEnvironment of the running execution context. During execution of ECMAScript code, the syntactic production PrimaryExpression : Identifier is evaluated using the following algorithm:

  1. Let env be the running execution context‘s LexicalEnvironment.
  2. If the syntactic production that is being evaluated is contained in a strict mode code, then let strict be true, else let strict be false.
  3. Return the result of calling GetIdentifierReference function passing env, Identifier, and strict as arguments.

The result of evaluating an identifier is always a value of type Reference with its referenced name component equal to the Identifier String.


Generally "scope" refers to the interval within which something - e.g., a variable - is in existence or is valid in some way.

"Context" sometimes refers to the set of objects accessible to an object operating with a particular framework as a matter of course - as part of the specification of that framework. That's what Matt provided. But sometimes it means something like "scope". "Within the scope of the current function" means pretty much the same as "within the context of the current function".

You can usually tell which meaning of context is meant from ... context.

I am not a Javascript expert!

0

精彩评论

暂无评论...
验证码 换一张
取 消