我创建了一个名为myClass
的Javascript类,其中包含两个方法:method1
和method2
。 method1
为名为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();
}
答案 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