Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从FB.api响应块调用javascript类方法_Javascript_Facebook_Oop_Facebook Javascript Sdk - Fatal编程技术网

无法从FB.api响应块调用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

我在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 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();
  });

};