JavaScript变量未定义与未定义 [英] JavaScript variable undefined vs not defined

查看:162
本文介绍了JavaScript变量未定义与未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML页面,附带以下JavaScript。

I have an HTML page with the following JavaScript attached.

alert(box);
box = "Thinking outside the box";

在控制台中我得到Uncaught ReferenceError:box not defined

In the console I get "Uncaught ReferenceError: box is not defined"

当我将其更改为:

alert(box);
var box = "Thinking outside the box";

警报被调用并显示未定义。我需要能够解释这一点,我对于为什么会发生这种情况有一个模糊的概念。我知道当我使用var时,JavaScript在执行警报之前知道变量是否存在,但是没有必要为它分配值?我离开这里了吗?需要一些帮助才能理解这一点。

The alert gets called and shows undefined. I need to be able to explain this, I have a vague idea of why this happens. I know that when I use var, JavaScript knows the variable is there before the alert is executed, but has not necessarily assigned a value to it?? Am I way off here? Need some help understanding this.

推荐答案

使用 var ,变量的声明被提升到范围的顶部,因此变量是为整个范围定义的。变量的初始化(分配它的初始值)保持在代码中的相同位置。

When you define a variable with var, the declaration of the variable is "hoisted" to the top of the scope and thus the variable is defined for the entire scope. The initialization of the variable (assigning it's initial value) remains at the same location in the code.

所以,在你的第二个例子中,当你做 alert(box),变量 box 已经被声明,因为已提升的 var 声明。你的第二个例子:

So, in your second example, when you do alert(box), the variable box has already been declared because of the hoisted var statement. Your second example:

alert(box);
var box = "Thinking outside the box";

基本等同于此(框的声明变量被提升到范围的顶部):

is basically equivalent to this (the declaration of the box variable is hoisted to the top of the scope):

var box;
alert(box);
box = "Thinking outside the box";

这使得变量被声明(尽管在 alert(box)语句之前没有初始化)因此你得到的结果与声明的变量一致,但是没有值( alert()报告 undefined 当变量存在但尚未初始化时会发生什么。

This makes the box variable declared (though not initialized) before your alert(box) statement and thus you get a result that is consistent with the variable being declared, but having no value yet (the alert() reports undefined which is what happens when the variable exists, but is not yet initialized).

您的第一个示例不使用 var ,因此在您执行 alert(框)时没有提升,所有名为的框都没有变量,因此你得到未被捕获的参考错误

Your first example does not use var and thus there is no hoisting so at the point where you do alert(box), there is no variable at all named box and thus you get the uncaught reference error.

SO上有许多帖子描述了吊装的细节。您可以在此处查看一长串列表: https://stackoverflow.com/search?q=javascript+variable+提升,你会发现变量提升的进一步解释。

There are many, many posts here on SO that describe the details of hoisting. You can see a long list of them here: https://stackoverflow.com/search?q=javascript+variable+hoisting where you will find further explanation of variable hoisting.

注意:函数声明也会被提升,所以你找到的一些帖子将是关于函数声明而不是比变量声明,虽然概念几乎相同。

Note: function declarations are also hoisted so some of the posts you find will be about function declarations rather than variable declarations, though the concept is pretty much the same.

这篇关于JavaScript变量未定义与未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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