- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我对解释器的理解是它逐行执行程序,我们可以看到即时结果,不像编译语言那样转换代码,然后执行它。
我的问题是,在 Javascript 中,解释器如何知道程序中某处声明了一个变量并将其记录为 undefined
?
考虑下面的程序:
function do_something() {
console.log(bar); // undefined (but in my understanding about an interpreter, it should be throwing error like variable not declared)
var bar = 111;
console.log(bar); // 111
}
function do_something() {
var bar;
console.log(bar); // undefined
bar = 111;
console.log(bar); // 111
}
最佳答案
这个概念' var
如果你从表面上想起来,“提升”是一个相当令人困惑的问题。你必须深入研究语言本身是如何工作的。 JavaScript 是 ECMAScript 的一种实现,是一种解释型语言,这意味着您编写的所有代码都会被输入到另一个程序中,该程序反过来解释代码,根据源代码的一部分调用某些函数。
例如,如果你写:
function foo() {}
解释器一旦满足你的函数声明,就会调用它自己的一个名为
FunctionDeclarationInstantiation
的函数。创建函数。解释器不是将 JavaScript 编译成本地机器代码,而是在读取 JavaScript 代码的每个部分时“按需”执行 C、C++ 和自己的机器代码。它不一定意味着逐行,所有解释都意味着不会编译为机器代码。执行机器代码的单独程序读取您的代码并即时执行该机器代码。
var
有何关系声明提升或任何与此相关的声明是解释器首先通读所有代码一次而不执行任何实际代码。它分析代码并将其分成块,称为
词法环境 .
Per the ECMAScript 2015 Language Specification :
8.1 Lexical Environments
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 BlockStatement, or a Catch clause of a TryStatement and a new Lexical Environment is created each time such code is evaluated.
An Environment Record records the identifier bindings that are created within the scope of its associated Lexical Environment. It is referred to as the Lexical Environment’s EnvironmentRecord
this
值(value)等。
8.2 Code Realms
Before it is evaluated, all ECMAScript code must be associated with a Realm. Conceptually, a realm consists of a set of intrinsic objects, an ECMAScript global environment, all of the ECMAScript code that is loaded within the scope of that global environment, and other associated state and resources.
this
领域的对象,领域的词汇环境,等等。
this
是等。在
var
的情况下声明,当你像这里一样定义一个函数时,就会创建一个领域:
function do_something() {
console.log(bar); // undefined
var bar = 111;
console.log(bar); // 111
}
在这里,一个 FunctionDeclaration 创建了一个新的词法环境,与一个新的领域相关联。创建词法环境后,解释器会分析代码并找到所有声明。然后首先在该词法环境的最开始处处理这些声明,因此是函数的“顶部”:
13.3.2 Variable Statement
A
var
statement declares variables that are scoped to the running execution context’s VariableEnvironment. Var variables are created when their containing Lexical Environment is instantiated and are initialized to undefined when created.
var
声明被创建,初始化为
undefined
.这意味着它们在执行任何代码之前被处理,在词法环境的“顶部”:
var bar; //Processed and declared first
console.log(bar);
bar = 111;
console.log(bar);
然后,在分析完所有 JavaScript 代码后,它最终会被执行。因为声明首先被处理,它被声明(并初始化为
undefined
)给你
undefined
.
let
和
const
以同样的方式行事,也被吊起来,但这行不通:
function do_something() {
console.log(bar); //ReferenceError
let bar = 111;
console.log(bar);
}
这将为您提供一个 ReferenceError 以尝试访问未初始化的变量。即使
let
和
const
声明被提升,规范明确指出你不能在初始化之前访问它们,不像
var
:
13.3.1 Let and Const Declarations
let
andconst
declarations define variables that are scoped to the running execution context’s LexicalEnvironment. The variables are created when their containing Lexical Environment is instantiated but may not be accessed in any way until the variable’s LexicalBinding is evaluated.
var
那样“在声明之前访问它”。 .
关于javascript - 如果 JavaScript 是一种解释性语言,提升如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45620041/
我在一个项目中工作,该项目需要 SQL 结果的最佳性能,并且希望优化查询,但经过反复试验后,我在 IN 方面遇到了一些问题。 -- THIS RETURNS NO RESULTS AT ALL. SE
在尝试创建一个实际上非常简单的 SQL 语句时,我发现自己迷失了方向。 我有一个包含 3 个表的数据库: 食谱 - 存储一些用于 cooking 的食谱名称 配料食谱 - 将配料与食谱链接 成分 -
我正在尝试理解 PHP 中的 Hebrev 函数。 https://php.net/manual/en/function.hebrevc.php 它说:“将逻辑希伯来语文本转换为视觉文本”。但我不明白
嗨,我在 Grid view 的 android 文档中发现了一段代码对于以下代码。 gridview.setOnItemClickListener(new OnItemClickListener()
谁能解释一下 InfiniBand 是什么?与以太网相比的主要区别是什么,这些差异如何使其比以太网更快? 在官方description从 mellanox 写到 Introduce InfiniBan
这个问题已经有答案了: How are java increment statements evaluated in complex expressions (1 个回答) 已关闭 8 年前。 我知道
我正在阅读 MySQL 教程,我遇到了这个: SELECT /*! SQL_NO_CACHE */ user FROM users; 为什么优化提示 SQL_NO_CACHE 包含在: /*!
我无法理解$(this),我做了一个剪刀石头布的版本,并应用了 jQuery 让用户在计算机上选择按钮选项。我希望有人能解释一下 $(this) 指的是什么,它是 btn-primary 吗?该函数在
我不是很确定 while(choice == 1 || choice ==2);谁能解释一下。我明白这一点 if(choice ==1) displayMonthly(rainfall); e
let flyRight = CABasicAnimation(keyPath: "position.x") flyRight.toValue = view.bounds.size.width/2 f
目录 解释:int型默认值为0 但我们尝试发现并不能通过: 原因: int的默认值为0,而Integer的默认值为null
我正在处理一个查询,自从一个 SSRS 服务器传输到另一个服务器后,它似乎没有按预期执行,并且 where 语句的一部分中出现了以下行 找出不同之处,或者至少从我能找到的地方来看。 where COA
我正在制作一个退回检测程序,读取退回邮件。我们的设置是发送电子邮件,在发送的邮件中添加一个 noreply@domain.tl。一些收件人不再存在,因此我们想要读取退回邮件,并检测它发送给谁。我已经崩
我有一个关于公式通过控制点弯曲的问题。 如您所知,HTML Canvas 有 quadraticCurveTo(x1, y1, x2, y2)与 x1 and x2作为控制点。 但是,当您尝试使用它绘
我有一个 Emakefile看起来像: %% -- %% %% -- {'/Users/user/projects/custom_test/trunk/*', [debug_info, {out
我有一个非常简单的问题。这不仅适用于 spray-json,而且我已经阅读了 argonaut 和 circe 的类似声明。所以请赐教。 在 spray-json 中,我遇到了 There is no
我正在为视频添加水印。我试图让水印与视频尺寸成比例。我已经使用 scale2ref 看到了十几个不同的答案,但没有解释实际发生了什么,所以我发现很难知道如何实现/更改配置以适应我的情况。 当前覆盖命令
因为我正在学习语言,所以我在玩 Haskell,我只是发现了一些我不理解的东西,我找不到解释。如果我尝试运行此代码: map (`div` 0) [1,2,3,4] 我得到一个除以 0 的异常,这是预
我正在寻找解决错误对象引用未设置到对象实例的步骤/指南。以及问题发生原因的解释。 我正在寻找更一般的解释,所以如果我收到错误,我应该采取什么步骤来查找问题。我经常看到有人提供特定代码段的帖子,而其他人
我最近想升级我的知识React ,所以我从组件生命周期方法开始。让我好奇的第一件事是这个componentWillReceiveProps .所以,文档说当组件接收新的(不一定是更新的) Prop 时
我是一名优秀的程序员,十分优秀!