Javascript值没有变化

时间:2013-07-15 23:38:50

标签: javascript

我创建了一个名为myClass的Javascript类,其中包含两个方法:method1method2method1为名为X的属性设置值,method2为同一属性设置另一个值并提醒它。

问题是我运行代码而没有任何警报。 任何想法为什么会发生这种情况?

我的代码如下:

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();

}

1 个答案:

答案 0 :(得分: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