admin管理员组文章数量:1384622
So I'm trying to call a method in a TypeScript class from within an array's ForEach loop. However, it seems that I can't figure out how to scope in the right 'this' for the parent class.
What I want to do is call the getFeatureAmount method from the survey.answerKey.q2SelectedValues.forEach(function(value)){ ... }); like this:
export class CalculationService {
private _baseRate: BaseRate;
private _subtotalPlatform: number = 0;
constructor(){
this._baseRate = new BaseRate(125, 60);
};
//This is the method I'm trying to call
private getFeatureAmount = (value: string, sub: number): number => {
return sub += parseInt(value) * this._baseRate.local;
}
public calculate(survey: Survey){
let subtotal_ui: number = 0;
subtotal_ui = (parseInt(survey.answerKey.q1SelectedValues[0]) * 5);
survey.answerKey.q2SelectedValues.forEach(function(value){
subtotal_ui = this.getFeatureAmount(value, subtotal_ui); //ERROR HERE. 'this' is undefined
});
return subtotal_ui + this._subtotalPlatform;
}
}
But I get that 'this' is undefined and can't find the getFeatureAmount. As a temporary workaround, I have to call getFeatureAmount as a callback function.
private getFeatureAmount = (value: string): number => {
return this._subtotalPlatform += parseInt(value) * this._baseRate.local;
}
survey.answerKey.q2SelectedValues.forEach(this.getFeatureAmount);
It's just not what i really wanted to do. So I'm wondering is there any way to do this with a lambda ()=>{}?
So I'm trying to call a method in a TypeScript class from within an array's ForEach loop. However, it seems that I can't figure out how to scope in the right 'this' for the parent class.
What I want to do is call the getFeatureAmount method from the survey.answerKey.q2SelectedValues.forEach(function(value)){ ... }); like this:
export class CalculationService {
private _baseRate: BaseRate;
private _subtotalPlatform: number = 0;
constructor(){
this._baseRate = new BaseRate(125, 60);
};
//This is the method I'm trying to call
private getFeatureAmount = (value: string, sub: number): number => {
return sub += parseInt(value) * this._baseRate.local;
}
public calculate(survey: Survey){
let subtotal_ui: number = 0;
subtotal_ui = (parseInt(survey.answerKey.q1SelectedValues[0]) * 5);
survey.answerKey.q2SelectedValues.forEach(function(value){
subtotal_ui = this.getFeatureAmount(value, subtotal_ui); //ERROR HERE. 'this' is undefined
});
return subtotal_ui + this._subtotalPlatform;
}
}
But I get that 'this' is undefined and can't find the getFeatureAmount. As a temporary workaround, I have to call getFeatureAmount as a callback function.
private getFeatureAmount = (value: string): number => {
return this._subtotalPlatform += parseInt(value) * this._baseRate.local;
}
survey.answerKey.q2SelectedValues.forEach(this.getFeatureAmount);
It's just not what i really wanted to do. So I'm wondering is there any way to do this with a lambda ()=>{}?
Share Improve this question asked Sep 9, 2016 at 14:21 MastroMastro 1,4972 gold badges23 silver badges53 bronze badges2 Answers
Reset to default 6Try changing
survey.answerKey.q2SelectedValues.forEach(function(value){
subtotal_ui = this.getFeatureAmount(value, subtotal_ui); //ERROR HERE. 'this' is undefined
})
to
survey.answerKey.q2SelectedValues.forEach((value) => {
// now this will be refer to the instance of your CalculationService class
subtotal_ui = this.getFeatureAmount(value, subtotal_ui);
});
var o = {
one: function() { return this; },
two: () => { return this; },
three() { return this; },
four() { return function () { return this; }; },
five() { return () => { return this; }; }
}
o.one() === o
o.two() === window
o.three() === o
o.four()() === window
o.five()() === o
Don't declare methods with lambda syntax, since this
will not be the object/class. Return or use lambda functions as arguments, if you want this
to be the containing class.
本文标签:
版权声明:本文标题:javascript - How do I reference this parent method in my TypeScript class from an array's ForEach loop? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744466795a2607561.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论