像下面的一段代码,定义了一个function,最前面加上括号,这是什么意思呢?
小弟初学js,谁能解答下呢?
<script type="text/javascript">
   (function() {    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);  })();</script>

解决方案 »

  1.   

    加上括号,是为了执行该function
      

  2.   


    function 放到()里面了,为啥还要在最后加上空括号()呢?
      

  3.   

    <script type="text/javascript">
       (function() {    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);  }) () ;</script>
      

  4.   

    它应该就是导入js文件的意思
    <script language="javascript" src="../ModelFile/popdate.js" type="text/javascript"></script>
    比代码添加方便多了
      

  5.   

    定义完了执行
    等于
    function test(){
     //...
    }
    test();
      

  6.   

    这样写的目的是把变量“ga”、“s”封装在函数内成为局域变量,一能避免变量名字污染全局,二能提高变量访问速度(局域变量比全局变量引用速度快)。
      

  7.   

    fandelei1982的解释比较合理。
    看看下面的代码,谁能解释下这是神马情况麽?
    <html>
    <body>
    <script> 
    (
    function example(){ 
      alert(1);
    }
    )( alert(2));
    </script>
    </body>
    </html>
    <html>
    <body>
    <script> 
    (
    function example(){ 
      alert(1);
    }
    ) ;
    </script>
    </body>
    </html>