无法从FB.api响应块调用javascript类方法
我在javascript中定义了两个类,如下所示无法从FB.api响应块调用javascript类方法,javascript,facebook,oop,facebook-javascript-sdk,Javascript,Facebook,Oop,Facebook Javascript Sdk,我在javascript中定义了两个类,如下所示 function ParentClass(){ this.one = function(){ alert('inside one of parent'); }; this.two = function(){ alert('inside two of parent'); //this is just a skeleton of the actual FB.api implementa
function ParentClass(){
this.one = function(){
alert('inside one of parent');
};
this.two = function(){
alert('inside two of parent');
//this is just a skeleton of the actual FB.api implementation in my code
FB.api('/me', 'post', function(response){
this.one();
});
};
}
function ChildClass(){
ParentClass.call(this);
//overriding the one() in ParentClass
this.one = function(){
alert('inside one of child');
};
}
ChildClass.prototype = new ParentClass();
ChildClass.prototype.constructor = ChildClass;
var c = new ChildClass();
c.two();
最后一行调用父类的two()方法,然后调用由ChildCLass重写的one()方法。
我收到一个错误,说“this.one()未定义”。但是,当我将this.one()方法放在FB.api响应块之外时,该函数会得到完美的调用。我认为问题可能在于this.one()中的“this”指的是FB.api回调函数,而不是ChildClass。如何解决此问题?只需在FB调用之外的另一个变量中保存一份
此
this.two = function(){
alert('inside two of parent');
//this is just a skeleton of the actual FB.api implementation in my code
var self = this;
FB.api('/me', 'post', function(response){
self.one();
});
};