最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Variable from global scope not recognized in javascript - Stack Overflow

programmeradmin0浏览0评论

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 0
Add a ment  | 

6 Answers 6

Reset to default 3

The 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);    
});
发布评论

评论列表(0)

  1. 暂无评论