javascript变量未定义 [英] javascript variable is undefined

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

问题描述

首先,让我们看看代码。

First , let's see the code.

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

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

I think the result is 01 .but in fact , The result is 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();

是的,这次它按预期运行。 01 。我无法理解,为什么?

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

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

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();

结果是01。

所以,任何人都可以解释一下吗?

So , Can anyone explain this?

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

Thanks for share your viewpoints. I simplify this code

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

二:

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

三:

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


推荐答案

当你引用变量时在函数JS中,首先检查该变量是否在当前作用域中声明,即在该函数内。如果未找到,则查看包含范围。如果仍未找到它,则会在 next 范围内查找,依此类推,直到最终达到全局范围。 (请记住,你可以将函数嵌套在彼此之内,这样你就可以获得几个级别的包含范围,当然你的例子不会这样做。)

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 next scope 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.)

声明:

b=1;

没有 var 声明一个可在任何函数中访问的全局变量,之外,然后在第一个函数中,您还声明本地 b 。这称为可变阴影

without var declares a global variable that is accessible within any function, except that then in your first function you also declare a local b. This is called variable shadowing.

但是,你说,我在 document.write(b) b $ C>。在这里你正在进行声明吊装。在函数中任何地方声明的变量由JS解释器处理,就好像它已在函数顶部声明(即,它被提升到顶部),但是,任何值赋值发生到位。所以你的第一个函数实际上就像是这样执行:

"But", you say, "I declare the local b after document.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:

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

在第二个函数中使用 this.b ,你会发现这个指的是窗口 ,全局变量本质上是 window 的属性。所以你正在访问全局 b 并忽略本地的。

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

在你的第三个函数中你没有声明一个本地 b ,所以它引用了全局的。

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

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

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