[英]call prototype method inside callback function of prototype method
i have javascript class and 2 prototype methods 我有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");
}
inside loadProject
method, i'm calling an api using callback function to access data and if it is ifSuccess
then i want call the kk
prototype method. 在
loadProject
方法内部,我正在使用回调函数调用api来访问数据,如果是ifSuccess
那么我想调用kk
原型方法。 but it gives me this error. 但这给了我这个错误。
angular.js:12722 TypeError: self.kk is not a function
i try to create new instance to the same class and then call the kk
function. 我尝试为同一类创建新实例,然后调用
kk
函数。 then it is working 然后它正在工作
this.client.ifSuccess(function(data){ //sucess
var in = new proEdit();
in.kk();
})
is it happening because of the callback? 因为回调发生了吗? how can i prevent this.
我怎样才能防止这种情况。 Thank you
谢谢
As commented, you should use .bind
如前所述,您应该使用
.bind
Sample 样品
window.name = "Bar" var MyClass = function(name) { this.name = name; } MyClass.prototype.notify = function() { console.log(this.name) } var m = new MyClass('Foo'); setTimeout(m.notify, 2000); setTimeout(m.notify.bind(m), 2000);
you can change ths code like this: 您可以这样更改代码:
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");
}
Hope it helps! 希望能帮助到你!
finally found the answer. 终于找到答案了。 as mention in the comment, need to use the
bind
to pass the this
to callback 如评论中所述,需要使用
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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.