两个相同的名称变量

时间:2011-09-22 14:12:53

标签: javascript variables

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

有两个var Khan个变量。怎么会?他们互相影响吗?

2 个答案:

答案 0 :(得分:8)

一个Khan是全局变量“Khan”的名称,另一个是自执行函数中的变量,它等于。

var Khan = (function(){

    ....

    var Khan = ...

    ....

})();

源文件中的缩进很糟糕,你可能没注意到......

答案 1 :(得分:2)

包含在匿名函数中的

变量只能在该函数内部工作。

所以这应该可行。

<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>
相关问题