2013-07-15 39 views
-3

我創建了一個名爲myClass的Javascript類,其中有兩種方法:method1method2method1將值設置爲名爲X的屬性,method2將另一個值設置爲相同的屬性併發出警報。Javascript的值不變

問題是我運行的代碼沒有任何警報。 任何想法爲什麼會發生這種情況?

我的代碼如下:

var myClass = function(classParamObject){ 

    this.method1 = function() { 

     classParamObject.X = 'TEST'; 

    }; 

    this.method2 = function() { 

     // Even if I change the value of X to 'NEW VALUE', it is not changing 
     classParamObject.X = 'NEW VALUE'; 

     // This alerts as 'TEST' 
     alert(classParamObject.X); 
    }; 

    this.method1(); 
    this.method2(); 

} 
+0

我沒有看到這個詞新的任何地方。除了文字。 –

+0

你是否從某個地方打電話給你的班級? –

+1

三個字母:O_o – jsalonen

回答

0

你必須實例化類整件事運行:(working jsFiddle

var myClass = function(classParamObject){ 

    this.method1 = function() { 

     classParamObject.X = 'TEST'; 

    }; 

    this.method2 = function() { 

     // Even if I change the value of X to 'NEW VALUE', it is not changing 
     classParamObject.X = 'NEW VALUE'; 

     // This alerts as 'TEST' 
     alert(classParamObject.X); 
    }; 

    this.method1(); 
    this.method2(); 

} 

// Now the important part: 
var myTest = new myClass({}); // initiate with object