javascript 变量未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11963240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:51:26  来源:igfitidea点击:

javascript variable is undefined

javascript

提问by SeasonHuang

First , let's see the code.

首先,让我们看看代码。

var a=0;
b=1;
document.write(a);
function run(){
    document.write(b);
    var b=1;
}
run();

I think the result is 01.but in fact , The result is 0undefined.

我认为结果是。01但实际上,结果是0undefined

Then I modify this code.

然后我修改了这段代码。

var a=0;
b=1;
document.write(a);
function run(){
    document.write(this.b); //or document.write(window.b)
    var b=1;
}
run();

Yeah, this time it runs as expected. 01. I can't understand, WHY?

是的,这次它按预期运行。01. 我不明白,为什么?

More interesting, I modify the code again .

更有趣的是,我再次修改了代码。

var a=0;
b=1;
document.write(a);
function run(){
    document.write(b);
    //var b=1;       //I comment this line
}
run();

The result is 01.

结果是01。

So , Can anyone explain this?

所以,谁能解释一下?

Thanks for share your viewpoints. I simplify this code

感谢您分享您的观点。我简化了这段代码

b=1;
function run(){
    console.log(b); //1
}

two:

二:

b=1;
function run(){
    var b=2;
    console.log(b); //2
}

three:

三:

b=1;
function run(){
    console.log(b); //undefined
    var b=2;
}

回答by nnnnnn

When you refer to a variable within a function JS first checks if that variable is declared in the current scope, i.e., within that function. If not found it looks in the containing scope. If still not found it looks in the nextscope up, and so forth until finally it reaches the global scope. (Bear in mind that you can nest functions inside each other, so that's how you get several levels of containing scope though of course your exmaple doesn't do that.)

当您在函数中引用一个变量时,JS 首先检查该变量是否在当前范围内声明,即在该函数内。如果未找到,它将在包含范围内查找。如果仍然没有找到,它会在下一个范围内查找,依此类推,直到最终到达全局范围。(请记住,您可以将函数相互嵌套,这样您就可以获得多个级别的包含范围,尽管您的示例当然不会这样做。)

The statement:

该声明:

b=1;

withoutvardeclares a global variable that is accessible within any function, exceptthat then in your first function you also declare a local b. This is called variable shadowing.

withoutvar声明了一个可以在任何函数中访问的全局变量,除了在你的第一个函数中你还声明了一个 local b。这称为变量阴影

"But", you say, "I declare the local bafterdocument.write(b)". Here you are running into declaration "hoisting". A variable declared anywhere in a function is treated by the JS interpreter as if it had been declared at the top of the function (i.e., it is "hoisted" to the top), but, any value assignment happens in place. So your first function is actually executed as if it was like this:

“但是”,你说,“我在b之后声明本地document.write(b)”。在这里,您遇到了“提升”声明。JS 解释器将在函数中任何位置声明的变量视为已在函数顶部声明(即,它被“提升”到顶部),但是,任何值分配都发生在适当的位置。所以你的第一个函数实际上是这样执行的:

function run(){
    var b;              // defaults to undefined
    document.write(b);  // write value of local b
    b=1;                // set value of local b
}

In your second function when you use this.b, you'll find that thisrefers to window, and global variables are essentially properties of window. So you are accessing the global band ignoring the local one.

在使用 的第二个函数中this.b,您会发现 是thiswindow,而全局变量本质上是 的属性window。因此,您正在访问全局b并忽略本地。

In your third function you don't declare a local bat all so it references the global one.

在您的第三个函数中,您根本没有声明本地函数b,因此它引用了全局函数。

回答by SLaks

When you write b = 1, you're creating a property in the global object.
In an ordinary function, bwill refer to this global.

当您编写 时b = 1,您是在全局对象中创建一个属性。
在一个普通的函数中,b会引用这个全局。

Since your function contains var b;, bwithin the function refers to the local variable. (varstatements create local variables throughout the function, no matter where the varis).
However, the executable portion of the varstatement (b = 1) is only executed at that point.

由于您的函数包含var b;,因此b在函数内指的是局部变量。(var语句在整个函数中创建局部变量,无论在哪里var)。
但是,var语句 ( b = 1)的可执行部分仅在该点执行。

回答by xdazz

The var directive is processed on the pre-execution stage, the bbecomes local variable before document.write(b);, so at that time, bis undefined.

var 指令在预执行阶段处理,在 之前b成为局部变量document.write(b);,所以当时bundefined

Note:assign a value to a variable is at the execution time, so you could image your code is like below.

注意:在执行时为变量赋值,因此您可以想象您的代码如下所示。

function run(){
? ? document.write(b);
? ? var b=1;
}

is the same as:

是相同的:

function run(){
    var b;
    document.write(b);
    b=1;
}

Addtion:

补充:

This is why Put every var definition at the top of the functionis good practice.

这就是为什么将每个 var 定义放在函数顶部是一种很好的做法。

回答by sandeep kumar

This code work fine for me

这段代码对我来说很好用

  if(var_name === "undefined"){
     alert(var_name+' : undefined');
   }