我想知道我们如何在javascript中读取另一个类/类实例中的局部变量的值。
例如:
我在一个类中有一个方法:
myClass1.prototype.myMethod0 = function()
{
this._myVar = null; //initialize this._myVar
}
myClass1.prototype.myMethod1 = function(list)
{
this._myVar = msg.list;
}
和
myClass1.prototype.myMethod2 = function()
{
//do something
// and update the list like say:
list1 = this._myVar; //access the this_myVar.
}
在我的另一个课程中说,
myClass2.prototype.myMethod = function()
{
//call the class1's method here..
myClass1.prototype.myMethod2();
}
myMethod2
是一个回调,我将它绑定在myClass2
.
这意味着,实际上,myMethod2
是这样调用的:myClass1.callback();
但我的问题是,当我打电话时myClass1.prototype.myMethod2();
,list1 = this._myVar;
它没有得到更新,它正在变成undefined
。我没有得到相同的修复。
问题是变量,“this._myVar”在 myclass1 的 myMethod2 中是“未定义”