- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
考虑三种情况,其中 a
和 k
都未定义:
if (a) console.log(1); // ReferenceError
和
var a = k || "value"; // ReferenceError
似乎有道理,但是......
var a = a || "value"; // "value"
为什么最后一个案例没有抛出一个ReferenceError
? a
不是在定义之前被引用了吗?
最佳答案
这是因为 var
的一项“功能”称为 hoisting .根据链接:
Because variable declarations (and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called "hoisting", as it appears that the variable declaration is moved to the top of the function or global code. (emphasis mine)
所以,例如:
console.log(a);
var a = "foo";
由于 a
在定义之前被引用,因此它不会像您预期的那样抛出 ReferenceError
,而是记录 undefined
。这是因为,如前所述,首先处理声明并且基本上发生在顶部,这意味着它与:
var a;
console.log(a);
a = "foo";
前面提到的函数也是如此:
function foo() {
console.log(a);
var a = "foo";
}
这等同于:
function foo() {
var a;
console.log(a);
a = "foo";
}
要了解原因,请查看 ECMAScript 2015 Language Specification :
13.3.2 Variable Statement
NOTE
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 toundefined
when created.[...]
A variable defined by a
VariableDeclaration
with anInitializer
is assigned the value of itsInitializer
’sAssignmentExpression
when theVariableDeclaration
is executed, not when the variable is created. (emphasis mine)
据此,我们可以了解到,var
声明是在任何代码执行之前创建的(在它们的词法环境中),并且范围限定为包含 VariableEnvironment
,它要么在全局范围内,要么在函数中。它们最初被赋予值 undefined
。下一部分解释了 var
赋值的值是执行声明时右侧的值,而不是创建变量时的值。
这适用于您的情况,因为在您的示例中,a
的引用与示例中的一样。使用之前的信息,您的代码:
var a = a || "value";
可以重写为:
var a;
a = a || "value";
请记住,所有声明都是在执行任何代码之前处理的。 JavaScript 引擎发现有一个声明,即变量 a
并将其声明在当前函数或全局范围的顶部。然后它被赋予值 undefined
。由于 undefined
是假的,a
被分配给 value
。
相比之下,您的第二个示例抛出一个ReferenceError
:
var a = k || "value";
也可以改写为:
var a;
a = k || "value";
现在你看到问题了。由于 k
从未在任何地方声明过,因此不存在具有该标识符的变量。这意味着,与第一个示例中的 a
不同,k
从未声明并抛出 ReferenceError
,因为它在声明之前被引用。
But how do you explain
var a = "123"; var a = a || "124"; // a = "123"
?
再次来自 ES2015 语言规范:
Within the scope of any VariableEnvironment a common
BindingIdentifier
may appear in more than oneVariableDeclaration
but those declarations collective define only one variable.
说白了:变量可以在同一个函数或全局范围内定义不止一次,但总是定义同一个变量。因此,在您的示例中:
var a = "123";
var a = a || "124";
可以重写为:
var a;
a = "123";
a = a || "124";
声明 a
在同一个函数或全局范围内 再次共同只声明一次。 a
被分配给 "123"
,然后又被分配给 "123"
因为 "123"
是真实。
从 ES2015 开始,在我看来,你应该不再使用 var
。它具有函数作用域,可能会导致问题中提到的意外分配。相反,如果您仍然想要可变性,请尝试使用 let
:
let a = a || "value";
这将抛出一个ReferenceError
。即使所有变量都被提升,无论您使用哪个声明符(var
、let
或 const
),对于 let
和 const
引用未初始化的变量是无效的。此外,let
和 const
具有 block 作用域,而不是函数作用域。这对于其他语言来说更加清晰和正常:
function foo() {
{
var a = 3;
}
console.log(a); //logs 3
}
对比:
function foo() {
{
let a = 3;
}
console.log(a); //ReferenceError
}
关于javascript - 为什么这个赋值不抛出 ReferenceError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41436194/
描述:。在我的Vue 3和nuxt 3项目中,我使用VITEST进行测试。但我在运行测试时遇到了一个问题:。版本。以下是我的页面代码:。下面是我的测试myest.spec.ts:。以下是我的vites
你好吗?我正在研究带有 Ionic 3 教程的 Google map 。我已经完成了那里解释的一切,但是当项目启动时,出现了这个错误。我调查了很多,但没有任何效果。谢谢! Error: Uncaugh
我刚刚开始使用 Node.js 并尝试使用模块。我已经安装了 Node 和 npm,并确保一切顺利。我将在下面添加代码来向您展示我得到了什么。 我有两个 js 文件,它们在这里。 app.js: va
我是 Angularjs 的初学者,在理解模块和作用域方面有些困难。 我不断收到范围未定义的错误,但我不明白为什么。首先,我将 Controller 链接到我设置路由的位置,但是由于在单击提交按钮时调
我正在制作一个 HTML 模板引擎。 程序遍历 HTML,并找到 {{ }} 的所有实例。然后它会遍历计数器中的所有键,并用对 Controller 的引用替换变量名称的实例。 例如,如果我的 Con
在我的 React Native 应用程序中,我有一个 ProductScreen,其中导入两个文件来完成屏幕。 文件 1 (Products.js) 是我的产品列表 文件 2 (Data.js) 是
我有以下类(class): class ScheduledContent { constructor(knex) { this.knex = knex } ge
在以下代码中,obj.sayhello() 行意味着sayhello在 obj 的上下文/范围内执行。 str未在 var 中定义(通过 sayhello )方法。 JS 将在范围内查找变量,即在
我正在尝试使用字符串作为函数的一部分。 const import1 = require('./import1'); const import2 = require('./import2'); //on
var g = { lang: "ttt", l: function(){ console.log(lang); } } console.log(g.l());
$(document).ready(function(){ $('#name').val('Name1'); }); function clickMe(){ console.lo
我刚刚完成了 Meteor 包的开发。现在我想通过将它添加到新的 Meteor 应用程序来测试它: my_cool_package_name/package.js Package.on_use(fun
我在给定的 SSCCE 代码中收到以下错误: Error: ReferenceError: electron is not defined Source File: http://localhost/
我正在关注一个简单的 tut,它有望构建一个响应式 slider ,但是它一直告诉我控制台中有一个错误指向一个名为 advance() 的函数 这是 slider 的简单 html:
我正在尝试使用基于按钮点击的表格中的数据填充表格。我正在将数据传递到 javascript 函数中。 )" id="modifyEdu" class="btn btn-primary"
我需要能够检查一个变量是否存在(如果它没有分配给 {})而不在 javascript 中抛出错误。当我尝试这段代码时 if (a) {} 它抛出 Uncaught ReferenceError
我在设置中有一个 mochajs 测试文件和一个 javascript 代码文件,如下所示: /js/module/codefile.js /js/test/testfile.js codefile.
我正在尝试构建一个简单的 Bookmarklet,它将一些外部 Javascript 加载到页面中,目标是将像 Stackoverflow 那样的 Markdown 编辑器添加到另一个站点。我可以使用
我有一个基于 Django 的网络应用程序。我用 Scrapy Crawler抓取网页。目前,我的目标是能够使用 jQuery 和 AJAX 请求从网页内控制爬虫。 我的理论设置如下: 在网页上,我有
以下 JavaScript... if (eval('typeof admin_post_css_theme_dark-moon)=='function')) {/**/} ...触发以下错误消息..
我是一名优秀的程序员,十分优秀!