Possible Duplicate:
Javascript global variables
Should I use window.variable or var?
Problem: Two ways to define global variables :
- var someVariablein global scope ;
- window["someVariable"] = “some value”;What's the difference ?
In my tests, the two ways a different in IE( from IE6 to IE8). (IE9 is OK) You may view it in my blog: ie-naming3.html, or run the following code:
<!doctype html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Test naming in IE6</title>
    <style type="text/css">
    </style>
    <script type="text/javascript">
            window.foo = window.foo || {};
            foo.eat = function(){
                alert("ie6");
            };
    </script>
</head>
<body>
    <div id="container">
    </div>
    <script type="text/javascript">
        alert(typeof window.foo.eat);
    </script>
    <!--   <script type="text/javascript" src="./ie6-naming.js"></script> -->
    <script>
//        alert(typeof window.foo.eat);
var foo = foo || {};      
        alert(typeof foo.eat);
    </script>
</body>
</html>
Any ideas are appreciated!
EDIT:
The problem is: run the code, you get two alerts: first show you "function", but the second show you "undefined", why?
 
     
    