开发者

Two same name variables

开发者 https://www.devze.com 2023-04-06 13:26 出处:网络
In https://github.com/Khan/khan-exercises/blob/master/khan-exercise.js there are two var Khan variables. How come? Do th开发者_JS百科ey affect each other?One Khan is the name of the global variable \

In https://github.com/Khan/khan-exercises/blob/master/khan-exercise.js

there are two var Khan variables. How come? Do th开发者_JS百科ey affect each other?


One Khan is the name of the global variable "Khan", the other is a variable inside the self executing function that it is equal to.

var Khan = (function(){

    ....

    var Khan = ...

    ....

})();

The indentation in the source file is horrible and you probably did not notice that....


variables wrapped in anonymous functions only work inside that function.

So this should work okay.

<script type="text/javascript">
$(function(){
   var khan = (function(){
        var khan = //this should not be a problem and they both work, this will be only available in the function
   }); 
});
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消