-3

I've created a Javascript class called myClass with two methods in it: method1 and method2. method1 sets a value to a property called X, method2 sets another value to the same property and alerts it.

The problem is I run the code and nothing alerts. Any ideas why is this happening?

My code is below:

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

}
4

1 回答 1

0

您必须实例化您的类才能运行整个程序:(工作 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
于 2013-07-15T23:41:59.667 回答