Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 试图理解对象原型_Javascript_Oop - Fatal编程技术网

Javascript 试图理解对象原型

Javascript 试图理解对象原型,javascript,oop,Javascript,Oop,为什么这段代码会给我一个错误“UncaughtTypeError:无法设置未定义的属性'GrossPerannuation'”?谢谢 function taxCalculation(configuration){ this.superAnnuationPercentage = configuration.superAnnuationPercentage; this.superAnnuationTaxRate = configuration.superAnnuatio

为什么这段代码会给我一个错误“UncaughtTypeError:无法设置未定义的属性'GrossPerannuation'”?谢谢

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;

};

您要更改的原型是您的构造函数之一:

taxCalculation.prototype.grossSuperAnnuation = function(income){
如果确实要从实例开始,可以执行以下操作:

tax.constructor.prototype.grossSuperAnnuation = function(income){
请注意,您可以这样做


但是,只有这个实例具有该函数,而不是使用
newtaxcalculation

创建的其他实例,因为原型是构造函数的属性。但每个对象都有指向其构造函数的链接。因此,您可以执行以下操作:
tax.constructor.prototype.grossuperannuation=function(){…}
不确定为什么要在实例上设置共享行为。你想把你自己和未来的程序员搞混吗?有关构造函数和原型的更多信息,请参见:
tax.grossSuperAnnuation = function(income){