我有以下两个类:
Class1 = function(){
this.allowReq = false;
}
Class1.prototype = {
constructor : Class1,
load : function(callBack){
if(this.allowReq)
callBack("Hello world");
else
console.log("Request Not allowed.");
}
}
Class2 = function(){
this.info = "";
}
Class2.prototype = {
constructor : Class2,
load : function(){
var obj1 = new Class1();
obj1.allowReq = true;
obj1.load(this.parse);
},
printInfo : function(){
alert(this.info);
},
parse : function(text){
this.info = text;
}
}
obj2 = new Class2();
obj2.load();
obj2.printInfo();
当我调用obj2.printInfo()时,我希望将此对象的info变量更改为" Hello world"。但是,它不受影响,但来自全局范围的信息变量(如果有)变为" Hello world"。有没有办法保留范围,以便obj2.info受到影响?