有一個構(gòu)造器方法用于構(gòu)建記錄成績的對象,對象原型中含有添加成績,顯示平均成績的方法,對于一個數(shù)組,通過forEach()迭代方法,傳入添加成績的方法,目的在于對沒個成績調(diào)用添加成績方法,然而我測試,報錯就是this.scores.push(score)那里can't read property 'scores' of undefined;代碼:function Score(){ this.scores = [];
}
Score.prototype.add = function(score){ this.scores.push(score);
};
Score.prototype.showAverage = function(){ let sum = this.scores.reduce(function(pre,cur){ return pre+cur;
}); console.log(sum*1.0/this.scores.length);
};let scores = [90,80,70];let score1 = new Score();
scores.forEach(score1.add);
score1.showAverage();請問這是什么問題呢,求解答
js數(shù)組迭代方法
手掌心
2018-08-14 09:10:43