(function(){
//do something
})();如题.没见过这样写的(function(){})(); ???

解决方案 »

  1.   

    定义完了直接执行
    等于下面这样写function a()
    {
        //do something
    }
    a();
      

  2.   


    还是没明白?你是说:
    (function(){//do something})();
    与function a()
    {
    //do something
    }a();效果一样?对吗?
      

  3.   

    试了一下,确实是这样的.那为什么不直接这样写呢:
    <script>
    //do something
    </script>而要写成这样:
    <script>
    (function(){//do something})();
    </script>
    一:
    <script>
    //do something
    </script>二:
    <script>
    (function(){//do something})();
    </script>这两者有什么差别吗?
      

  4.   

    <script>
    var abc = "谁都可以用";
    (function(){
        var abc = '我就是不让别人用';
        alert(abc);
    })()
    alert(abc);
    </script>