本文介绍了为什么变量定义的全局是未定义的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好,我有一个简单的函数和一个全局变量。 var myname =global; //全局变量函数func(){ alert(myname); //undefined var myname =local; alert(myname); //本地} func(); 不可能引用在该函数范围外定义的外部变量吗?并在这个全局变量... 我怎样才能解决这个问题,所以我没有得到 undefined 从一个全局变量? 解决方案你刚刚偶然发现了一个叫做hoisting的js功能 var myname =global; //全局变量函数func(){ alert(myname); //undefined var myname =local; alert(myname); //本地} func(); 在定义 func 时,编译器查看函数体。它看到您正在声明一个名为 myname 的变量。因为吊起你的代码被重写为以下内容。 var myname =global; //全局变量函数func(){ var myname; //声明局部变量并将其赋值undefined alert(myname); //undefined myname =local; //将本地var myname分配给本地 alert(myname); //本地} func(); 这个覆盖全局变量。如果你想访问函数范围内的全局变量,可以使用 this 关键字。 var myname =global; //全局变量函数func(){ var myname =local; alert(this.myname); //全局 alert(myname); //本地} func(); 请注意,这只适用于调用函数而不是方法或构造函数,因为 编辑:为了完整性 如果您想在任何上下文中访问全局变量而不考虑函数类型,则声明一个通常不会涵盖的全局变量。 var global = this; //在全球范围内。 var myname =global; var obj = {f:function(){ var myname =local; console.log(global.myname); }}; obj.f(); //global 请注意,这是在方法位置, this 关键字直接指向obj,因此没有定义myname。 Hi guys here I have a simple function and a global variable.var myname = "global"; // global variablefunction 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 undefined from a global variable? 解决方案 You have just stumbled on a js "feature" called hoistingvar myname = "global"; // global variablefunction func() { alert(myname); // "undefined" var myname = "local"; alert(myname); // "local"}func();In this code when you define func the compiler looks at the function body. It sees that you are declaring a variable called myname.Because of hoisting your code is rewritten to the following.var myname = "global"; // global variablefunction 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 this keyword.var myname = "global"; // global variablefunction 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 this keyword changes what its bound to based on how you call a function.EDIT: For completenessIf 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 this keyword refers to obj directly and therefore doesn't have myname defined. 这篇关于为什么变量定义的全局是未定义的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-27 16:29