我今天看到这样的一个js代码
class5 = (function() { 
    // private static fields 
    var s_first = 1; 
    var s_second = 2; 
  
    // private static methods 
    function s_method1() { 
        s_first++; 
    } 
    var s_second = 2; 
  
    function constructor() { 
        // private fields 
        var m_first = 1; 
        var m_second = 2; 
  
        // private methods 
        function method1() { 
            alert(m_first); 
        } 
        var method2 = function() { 
            alert(m_second); 
        } 
  
        // public fields 
        this.first = "first"; 
        this.second = ['s','e','c','o','n','d']; 
  
        // public methods 
        this.method1 = function() { 
            s_second--; 
        } 
  
        this.method2 = function() { 
            alert(this.second); 
        } 
  
        // constructor 
        { 
            s_method1(); 
            this.method1(); 
        } 
    } 
    // public static methods 
    constructor.method1 = function() { 
        s_first++; 
        alert(s_first); 
    } 
    constructor.method2 = function() { 
        alert(s_second); 
    } 
  
    return constructor; 
})(); 在function 外面加个刮号是什么意思。后面那个()是干什么用的。请高手指点下。
  

解决方案 »

  1.   

    就是把function运行后的返回值赋值给class5例如:
    val = ( function(){ return 5 } )()
    结果就是val=5, 相当于:function fun()
    {
        return 5
    }val = fun()
      

  2.   

    class5 = (
    function() { 
        // private static fields 
        var s_first = 1; 
        var s_second = 2; 
      
        // private static methods 
        function s_method1() { 
            s_first++; 
        } 
        var s_second = 2; 
      
        function constructor() { 
            // private fields 
            var m_first = 1; 
            var m_second = 2; 
      
            // private methods 
            function method1() { 
                alert(m_first); 
            } 
            var method2 = function() { 
                alert(m_second); 
            } 
      
            // public fields 
            this.first = "first"; 
            this.second = ['s','e','c','o','n','d']; 
      
            // public methods 
            this.method1 = function() { 
                s_second--; 
            } 
      
            this.method2 = function() { 
                alert(this.second); 
            } 
      
            // constructor 
            { 
                s_method1(); 
                this.method1(); 
            } 
        } 
        // public static methods 
        constructor.method1 = function() { 
            s_first++; 
            alert(s_first); 
        } 
        constructor.method2 = function() { 
            alert(s_second); 
        } 
      
        return constructor; 
    }
    )(); 这样是不是容易看懂些,注意层次结构
      

  3.   

    你可以这样理解,
    ()跟在一个js 的 function后面,就相当于运算符。如一楼
    val = ( function(){ return 5 } )() //运算
    结果就是val=5, 相当于: function fun() 

        return 5 
    } val = fun()//运算
      

  4.   

    (function(style){
                style.width = style.height = "100%"; style.backgroundColor = "#fff"; style.filter = "alpha(opacity:0)";
            })(this.Drag.appendChild(document.createElement("div")).style)
    那这段代码是什么意思。还是不理解,比较笨。不好意思
      

  5.   

    事实上就等于 声明了class5 这个函数 并默认执行一次
    类似这个样
    var cms=(function(str){alert(str);})('我被声明了,我也被执行了');
      

  6.   

    不好意思错了 是声明一个匿名函数 并执行一次 用class5获取他的返回