admin管理员组文章数量:1419702
It's like this:
<script>
var global_var = {foo:1};
</script>
<script src="myscript.js"></script>
in myscript.js I have
jQuery(document).ready(function($){
var global_var = typeof global_var == 'undefined' ? {foo:2} : global_var;
console.log(global_var);
});
But the global_var does not seem to be recognized in my 2nd script, even though I clearly defined it in the first, like a global variable.
/
It's like this:
<script>
var global_var = {foo:1};
</script>
<script src="myscript.js"></script>
in myscript.js I have
jQuery(document).ready(function($){
var global_var = typeof global_var == 'undefined' ? {foo:2} : global_var;
console.log(global_var);
});
But the global_var does not seem to be recognized in my 2nd script, even though I clearly defined it in the first, like a global variable.
http://jsfiddle/tYErg/
Share Improve this question asked Jun 22, 2012 at 16:22 AlexAlex 66.2k185 gold badges460 silver badges651 bronze badges 06 Answers
Reset to default 3The problem is the "var" in "var global_var" within your jQuery function. This is creating a different "global_var" variable within your function instead of using the true global. Drop this and your code works as expected.
It's because you've declared another global_var
in the current scope. Either remove the var
or change the variable name:
eg
global_var = typeof global_var == 'undefined' ? {foo:2} : global_var;
var other_var = typeof global_var == 'undefined' ? {foo:2} : global_var;
http://jsfiddle/8Mptw/
You can't use two different variables with the same name from different scopes within the same scope. So when you declare your local global_var
with the var
keyword, your global bees inaccessible.
If you need to make a local variable with the same name as a global you can use a closure like:
jQuery(document).ready(function($){
(function(global_var){
// global_var is a local copy of global_var from the outer scope
global_var = typeof global_var == 'undefined' ? {foo:2} : global_var;
console.log(global_var);
})(global_var);
});
You can also refer to it as a property of the window
object if you know it's global:
jQuery(document).ready(function($){
var global_var = typeof window.global_var == 'undefined' ?
{foo:2} : window.global_var;
console.log(global_var);
});
Finally, if you don't want a local copy and just want to access the variable from the outer scope, then do not use the var
keyword, since the purpose of that keyword is declaring a new variable:
jQuery(document).ready(function($){
global_var = typeof global_var == 'undefined' ? {foo:2} : global_var;
console.log(global_var);
});
Try defining it without the var
key work to make it in the global scope
<script>
var global_var = {foo:1};
</script>
<script>
jQuery(document).ready(function($){
global_var = typeof global_var === 'undefined' ? {foo:2} : global_var;
alert(global_var.foo); // should be 1
});
</script>
http://jsfiddle/tYErg/3/
try this
jQuery(document).ready(function($){
global_var = typeof global_var == 'undefined' ? {foo:2} : global_var;
console.log(global_var);
});
You are having this problem because you are declaring a local variable with the same name as your global variable, therefore the global variable is not visible any more. You can still access it by explicitly specifying the scope:
jQuery(document).ready(function($){
var global_var = typeof window.global_var == 'undefined' ? {foo:2} : window.global_var;
console.log(global_var);
});
本文标签: jqueryVariable from global scope not recognized in javascriptStack Overflow
版权声明:本文标题:jquery - Variable from global scope not recognized in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745303839a2652531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论