为什么这段代码会给我一个错误“Uncaught TypeError:无法设置未定义的属性'grossSuperAnnuation'”?感谢。
function taxCalculation(configuration){
this.superAnnuationPercentage = configuration.superAnnuationPercentage;
this.superAnnuationTaxRate = configuration.superAnnuationTaxRate;
};
var tax = new taxCalculation({
superAnnuationPercentage: 9.25,
superAnnuationTaxRate: 15
});
tax.prototype.grossSuperAnnuation = function(income){
return income * this.superAnnuationPercentage / 100;
};
答案 0 :(得分:2)
您想要更改的原型是您的构造函数之一:
taxCalculation.prototype.grossSuperAnnuation = function(income){
如果你真的想从实例开始,你可以这样做:
tax.constructor.prototype.grossSuperAnnuation = function(income){
请注意,可以执行
tax.grossSuperAnnuation = function(income){
但是只有这个实例才具有该功能,而不是使用new taxCalculation
创建的其他实例。