函数表达式是否具有自己的范围/词法环境 [英] Does a function expression have its own scope/lexical environment

查看:111
本文介绍了函数表达式是否具有自己的范围/词法环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读的执行上下文/词法环境部分ECMA 262 5规范。它声明以下内容:(强调)

I'm reading the Execution Context / Lexical Environment section of the ECMA 262 5 specification. It states the following: (emphasis added)


词法环境是一种规范类型,用于定义标识符与特定变量和函数的关联基于ECMAScript代码的词法嵌套结构。词法环境由环境记录和对外部词法环境的可能为空的引用组成。通常,词汇环境与ECMAScript代码的某些特定语法结构(例如 FunctionDeclaration ,WithStatement或TryStatement的Catch子句)相关联,并且每次评估此类代码时都会创建一个新的词汇环境。

A Lexical Environment is a specification type used to define the association of Identifiers to specific variables and functions based upon the lexical nesting structure of ECMAScript code. A Lexical Environment consists of an Environment Record and a possibly null reference to an outer Lexical Environment. Usually a Lexical Environment is associated with some specific syntactic structure of ECMAScript code such as a FunctionDeclaration, a WithStatement, or a Catch clause of a TryStatement and a new Lexical Environment is created each time such code is evaluated.

我注意到它没有为函数表达式创建词法环境。是为函数表达式创建的词法环境,还是仅为函数声明创建的词法环境?我缺少什么吗?

I noticed that it says nothing about creating a lexical environment for Function Expressions. Is a lexical environment created for function expressions, or is one created only for function declarations? Am I missing something?

编辑:我注意到功能代码将具有其自己的执行上下文,这就是为什么我也感到困惑的原因,为什么在词法环境部分未提及函数表达式。 / p>

Edit: I notice that function code will have its own execution context, which is why I'm also confused why function expression is not mentioned in the lexical environment section.

推荐答案

是的,每个函数都会获取(第10.4.3 )当它自己的 ExecutionContext 时被称为(§13.2.1)。该新上下文使用新的 LexicalEnvironment (由 NewDeclarativeEnvironment 创建,§10.2.2.2),从 [[Scope]] -即 LexicalEnvironment 在(§13)。

Yes, every function gets (§10.4.3) its own ExecutionContext when it is called (§13.2.1). That new context is initialised with a new LexicalEnvironment (created by NewDeclarativeEnvironment, §10.2.2.2), deriving from the [[Scope]] of the function - i.e. the LexicalEnvironment it was declared/"expressed" in (§13).

正如@Pointy指出的那样,您绊倒的句子并未详尽列出它们: …某些[结构],例如…

As @Pointy pointed out, the sentence you stumpled upon does not exhaustively list them: "…some [structure] such as…".

这篇关于函数表达式是否具有自己的范围/词法环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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