I have defined two classes in javascript as follows.
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();
the last line calls the ParentClass's two() method which then calls the one() method overriden by the ChildCLass.
I get an error saying “this.one() is not defined”. But when I put the this.one() method outside the FB.api response block the function gets called perfectly. I think the problem might be that the ‘this’ in this.one() is referring to the FB.api callback function instead of the ChildClass. How do I solve this?
Just stash a copy of
thisin another variable outside the FB call.