Javascript 在prototype方法的回调函数中调用prototype方法

Javascript 在prototype方法的回调函数中调用prototype方法,javascript,angularjs,Javascript,Angularjs,我有javascript类和2个原型方法 var proEdit = function(){ this.client; var self = this; } /* class method to load all the project releted details */ proEdit.prototype.loadProject = function(){ this.client = $serviceCall.setClient("getAllByProj

我有javascript类和2个原型方法

var proEdit = function(){
    this.client;
    var self = this;
}
/*
    class method to load all the project releted details 
*/
proEdit.prototype.loadProject = function(){
    this.client =  $serviceCall.setClient("getAllByProjectID","project"); // method name and service
    this.client.ifSuccess(function(data){  //sucess        
        self.kk();
    })
    this.client.ifError(function(data){ //falce
        console.log("error loading setting data")
    })
    this.client.projectID($stateParams.projectID); // send projectID as url parameters
    this.client.getReq(); // get request if post then use 'postReq('jsonbody')'
    return this;
}


proEdit.prototype.kk = function(){
    console.log("sass");
}
loadProject
方法中,我使用回调函数调用一个api来访问数据,如果它是
ifSuccess
,那么我想调用
kk
原型方法。但它给了我这个错误

angular.js:12722 TypeError:self.kk不是函数

我尝试为同一类创建新实例,然后调用
kk
函数。然后它就开始工作了

  this.client.ifSuccess(function(data){  //sucess  
      var in = new proEdit();   
      in.kk();
  })

这是因为回调吗?我怎样才能防止这种情况。谢谢

如评论所述,您应该使用
.bind

样本

window.name=“Bar”
var MyClass=函数(名称){
this.name=名称;
}
MyClass.prototype.notify=函数(){
console.log(this.name)
}
var m=新的MyClass('Foo');
setTimeout(m.notify,2000);

setTimeout(m.notify.bind(m),2000)如注释所示,您应该使用
.bind

样本

window.name=“Bar”
var MyClass=函数(名称){
this.name=名称;
}
MyClass.prototype.notify=函数(){
console.log(this.name)
}
var m=新的MyClass('Foo');
setTimeout(m.notify,2000);

setTimeout(m.notify.bind(m),2000)您可以这样更改代码:

var proEdit = function(){
    this.client;
    // var self = this;
}
/*
    class method to load all the project releted details 
*/
proEdit.prototype.loadProject = function(){
    var that = this;
    this.client =  $serviceCall.setClient("getAllByProjectID","project"); // method name and service
    this.client.ifSuccess(function(data){  //sucess        
        that.kk();
    })
    this.client.ifError(function(data){ //falce
        console.log("error loading setting data")
    })
    this.client.projectID($stateParams.projectID); // send projectID as url parameters
    this.client.getReq(); // get request if post then use 'postReq('jsonbody')'
    return this;
}


proEdit.prototype.kk = function(){
    console.log("sass");
}

希望有帮助

您可以这样更改代码:

var proEdit = function(){
    this.client;
    // var self = this;
}
/*
    class method to load all the project releted details 
*/
proEdit.prototype.loadProject = function(){
    var that = this;
    this.client =  $serviceCall.setClient("getAllByProjectID","project"); // method name and service
    this.client.ifSuccess(function(data){  //sucess        
        that.kk();
    })
    this.client.ifError(function(data){ //falce
        console.log("error loading setting data")
    })
    this.client.projectID($stateParams.projectID); // send projectID as url parameters
    this.client.getReq(); // get request if post then use 'postReq('jsonbody')'
    return this;
}


proEdit.prototype.kk = function(){
    console.log("sass");
}

希望有帮助

终于找到了答案。如注释中所述,需要使用
bind
this
传递给回调

var proEdit = function(){
    this.client;
    var self = this;
}
/*
    class method to load all the project releted details 
*/
proEdit.prototype.loadProject = function(){
    this.client =  $serviceCall.setClient("getAllByProjectID","project"); // method name and service
    this.client.ifSuccess((function(data){  //sucess
        console.log(data) 
        $scope.project = data.project[0];
        $scope.task = data.task;
        $scope.user = data.user;
        $scope.balance = data.balance[0];
        this.kk();
    }).bind(this))
    this.client.ifError(function(data){ //falce
        console.log("error loading setting data")
    })
    this.client.projectID($stateParams.projectID); // send projectID as url parameters
    this.client.getReq(); // get request if post then use 'postReq('jsonbody')'
    return this;
}


proEdit.prototype.kk = function(){
    console.log("sass");
}

终于找到了答案。如注释中所述,需要使用
bind
this
传递给回调

var proEdit = function(){
    this.client;
    var self = this;
}
/*
    class method to load all the project releted details 
*/
proEdit.prototype.loadProject = function(){
    this.client =  $serviceCall.setClient("getAllByProjectID","project"); // method name and service
    this.client.ifSuccess((function(data){  //sucess
        console.log(data) 
        $scope.project = data.project[0];
        $scope.task = data.task;
        $scope.user = data.user;
        $scope.balance = data.balance[0];
        this.kk();
    }).bind(this))
    this.client.ifError(function(data){ //falce
        console.log("error loading setting data")
    })
    this.client.projectID($stateParams.projectID); // send projectID as url parameters
    this.client.getReq(); // get request if post then use 'postReq('jsonbody')'
    return this;
}


proEdit.prototype.kk = function(){
    console.log("sass");
}

您可以使用
.bind
传递具有特定上下文的回调
callback.bind(this)
callback
中,您可以使用
访问this
您可以使用
.bind
传递具有特定上下文的回调
callback.bind(this)
并且在
callback
内部,您可以使用
this