admin管理员组文章数量:1279177
In a JavaScript environment can I declare a variable before a function to make the variable reachable on a global level. For instance:
var a;
function something(){
a = Math.random()
}
Will this make "a" a global variable?
or is using...
var a = function(){
var b = Math.random();
return b;
}
document.write(a())
Really the only way to do it?
Is there a way to make "b" global other than calling the function "a()"?
In a JavaScript environment can I declare a variable before a function to make the variable reachable on a global level. For instance:
var a;
function something(){
a = Math.random()
}
Will this make "a" a global variable?
or is using...
var a = function(){
var b = Math.random();
return b;
}
document.write(a())
Really the only way to do it?
Is there a way to make "b" global other than calling the function "a()"?
Share Improve this question edited Jan 14, 2013 at 4:29 steveax 17.7k6 gold badges46 silver badges59 bronze badges asked Jan 14, 2013 at 4:23 user1690995user16909953 Answers
Reset to default 10There are basically 3 ways to declare a global variable:
- Declaring it in the global scope, outside of any function scope.
- Explicitly assigning it as a property of the window object:
window.a = 'foo'
. - Not declaring it at all (not remended). If you omit the
var
keyword when you first use the variable, it'll be declared globally no matter where in your code that happens.
Note #1: When in strict mode, you'll get an error if you don't declare your variable (as in #3 above).
Note #2: Using the window
object to assign a global variable (as in #2 above) works fine in a browser environment, but might not work in other implementations (such as nodejs), since the global scope there is not a window
object. If you're using a different environment and want to explicitly assign your global variables, you'll have to be aware of what the global object is called.
Will this make "a" a global variable?
A var
declaration makes the variable local to the enclosing scope, which is usually a function one's. If you are executing your code in global scope, then a
will be a global variable. You could as well just omit the var
, then your variable would be implicitly global (though explicit declaration is better to show your intention).
Is there a way to make "b" global other than calling the function "a()"?
Your b
variable is always local to the function a
and will never leave it, unless you remove the var
.
Before you think of making a variable global scope, you should consider JavaScript global namespace pollution. The more global variables you declare, the more it is likely that you application will get into conflict with another application's namespace and break. As such, it is highly important minimize the number of global variables.
本文标签: javascriptDeclaring Empty Variables to make them globalStack Overflow
版权声明:本文标题:javascript - Declaring Empty Variables to make them global - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741244207a2364563.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论