function(){alert('sss')}
是個匿名函數。沒有名字。所以沒有辦法調用。
在外面加個括號,就變成了一個值,值的內容是函數的引用。例如
var a = (function(){"nop"})
a 就是對這個函數的引用。有了名字,之后可以調用,例如a()
現在省略了a,直接對()中的值進行調用就出現了()()的形式,第一個括號中是個函數,就是這樣。
如果還不懂,看看下面三段代碼試試:
?
<script>
(function(){
??function a(){
?? ?function b(){}
??}
??alert(typeof a); // a 可見
??alert(typeof b); // b 不可見
})()
</script>
?
?
<script>
(function(){
??function a(){
?? ?function b(){}
alert(typeof b);
??}
??a(); // b 可見
??alert(typeof a); // a 可見
??alert(typeof b); // b 不可見
})()
</script>
?
?
<script>
(function(){
??(function a(){
?? ?function b(){}
alert(typeof b); // b 可見
??})()
??alert(typeof a); // a 不可見
??alert(typeof b); // b 不可見
})()
</script>