Javascript 为什么定义的全局变量未定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30469755/
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-23 05:13:47  来源:igfitidea点击:

Why a variable defined global is undefined?

javascriptglobal-variableslocal-variableshoisting

提问by J Rod

Hi guys here I have a simple function and a global variable.

大家好,我有一个简单的函数和一个全局变量。

Why is mynameundefinedand not the string "global"?

为什么是mynameundefined而不是字符串"global"

var myname = "global"; // global variable
function func() {
    alert(myname); // "undefined"
    var myname = "local";
    alert(myname); // "local"
}
func();

Is not possible to refer to a outer variable that is define outside the scope of that function? and in this a global variable...

不能引用在该函数范围之外定义的外部变量吗?在这个全局变量中......

And how I can fix this so I don't get a undefinedfrom a global variable?

我该如何解决这个问题,这样我就不会undefined从全局变量中获得 a ?

回答by t3dodson

You have just stumbled on a js "feature" called hoisting

你刚刚偶然发现了一个叫做提升的 js“特性”

var myname = "global"; // global variable
function func() {
    alert(myname); // "undefined"
    var myname = "local";
    alert(myname); // "local"
}
func();

In this code when you define functhe compiler looks at the function body. It sees that you are declaring a variable called myname.

在这段代码中定义func编译器时会查看函数体。它看到您正在声明一个名为myname.

Javascript Hoistsvariable and function declarations, by moving the declaration to the top of the function.

的Javascript升降机变量和函数声明,该声明移动到函数的顶部。

Because of hoisting your code is rewritten to the following.

由于提升你的代码被重写为以下内容。

var myname = "global"; // global variable
function func() {
   var myname; //declare local variable and assign it undefined
   alert(myname); // "undefined"
   myname = "local"; // assign local var myname to "local"
   alert(myname); // "local"
}
func();

This "Covers" the global variable. If you want access to the global variable within the scope of a function use the thiskeyword.

这“覆盖”了全局变量。如果要访问函数范围内的全局变量,请使用this关键字。

var myname = "global"; // global variable
function func() {
    var myname = "local";
    alert(this.myname); // "global"
    alert(myname); // "local"
}
func();

Note that this only works in calling a function not a method or constructor because the thiskeyword changes what its bound to based on how you call a function.

请注意,这仅适用于调用函数而不是方法或构造函数,因为this关键字会根据您调用函数的方式更改其绑定的内容。

EDIT: For completeness

编辑:为了完整性

If you want to get access to global variables in any context regardless of function type then declare a global variable that by convention you never cover.

如果您想在任何上下文中访问全局变量而不管函数类型如何,请声明一个按照惯例您永远不会覆盖的全局变量。

var global = this; // in global scope.
var myname = "global";
var obj = {f: function () {
    var myname = "local";
    console.log(global.myname);
}};
obj.f(); // "global"

Note that this is in method position and the thiskeyword refers to obj directly and therefore doesn't have myname defined.

请注意,这是在方法位置,this关键字直接引用 obj,因此没有定义 myname。

回答by Joe Enos

Inside a function, you're declaring var myname = "local". Even though you're doing it in the middle of the method, that variable has function scope, so it belongs to the entire function, even the code above it.

在函数内部,您声明var myname = "local". 即使您在方法的中间执行此操作,该变量也具有函数作用域,因此它属于整个函数,甚至是其上方的代码。

So the local variable's value is undefined before that line, and has a value after, but neither one are touching the global variable.

所以局部变量的值在那行之前是未定义的,之后有一个值,但没有人接触全局变量。

回答by Andrew

The reason the first alert is undefined is because you re-declared globalas a local variable below it in the function. And in javascript that means from the top of the function it is considered the local variable.

第一个警报未定义的原因是因为您global在函数中重新声明为它下面的局部变量。在 javascript 中,这意味着从函数的顶部开始,它被认为是局部变量。

The one below it works because just above the alert you gave it a value.

它下面的那个有效,因为就在警报之上,你给了它一个值。

回答by DUUUDE123

You forget "var" in the first line:

您忘记了第一行中的“var”:

var myName = "global";

Hoisting simply refers to the fact that javascript goes through and sets all variables that are initialized to the value undefined (not a string)

提升只是指 javascript 遍历并设置所有初始化为 undefined 值的变量(不是字符串)