在JavaScript中向原型对象添加方法

在JavaScript中向原型对象添加方法,javascript,Javascript,我正在通过,我遇到了一个编译器错误,代码来自书中。我已经将我的语法与书中的语法进行了三次核对,我想我可以排除它。我也看过了,我没有找到我所在页面的参考资料 我也看了,但我没有发现任何适用于我的问题 这是我的对象构造函数: var Invoice = function(subtotal, taxRate) { this.subtotal = subtotal; this.taxRate = taxRate; }; 尝试向prototype对象添加方法时遇到问题: // The g

我正在通过,我遇到了一个编译器错误,代码来自书中。我已经将我的语法与书中的语法进行了三次核对,我想我可以排除它。我也看过了,我没有找到我所在页面的参考资料

我也看了,但我没有发现任何适用于我的问题

这是我的对象构造函数:

var Invoice = function(subtotal, taxRate) {
    this.subtotal = subtotal;
    this.taxRate = taxRate;
};
尝试向prototype对象添加方法时遇到问题:

// The getTaxAmount method is added to the Invoice prototype
Invoice.prototype.getTaxAmount: function() {
// Compiler whines right here ^ and     ^
    return (subtotal * this.taxRate);
};

// The getInvoiceTotal method is added to the Invoice prototype
Invoice.prototype.getInvoiceTotal: function() {
// Compiler whines right here ^ and     ^
    return subtotal + this.getTaxAmount(this.subtotal);
};
我正在使用VisualStudio代码和Chrome调试器。VS代码中的提示表示它需要一个在第一个投诉点,而
[js]标识符应在第二个投诉点

谢谢你的阅读。欢迎您的建议。

语法为

Invoice.prototype.getInvoiceTotal = function() {}
这是因为原型本身就是一个对象。

语法是

Invoice.prototype.getInvoiceTotal = function() {}
这是因为原型本身就是一个对象。

根据,向原型添加函数的正确方法如下:

Invoice.prototype.getInvoiceTotal=function(){**your fx here**}

从外观上看,您使用的是
而不是
=
,这可能是导致您悲伤的原因。

根据,向原型添加函数的正确方法如下:

Invoice.prototype.getInvoiceTotal=function(){**your fx here**}


从表面上看,你使用的是
而不是
=
,这可能是导致你悲伤的原因。

尝试使用等号。冒号表示JSON的变量值

Invoice.prototype.getInvoiceTotal = function() {
// Compiler whines right here ^ and     ^
    return subtotal + this.getTaxAmount(this.subtotal);
};

尝试使用等号。冒号表示JSON的变量值

Invoice.prototype.getInvoiceTotal = function() {
// Compiler whines right here ^ and     ^
    return subtotal + this.getTaxAmount(this.subtotal);
};

是的,这是一个语法错误。您更愿意使用赋值运算符,
=
,用于此类工作:

// The getTaxAmount method is added to the Invoice prototype
Invoice.prototype.getTaxAmount = function() {
    return (subtotal * this.taxRate);
};

// The getInvoiceTotal method is added to the Invoice prototype
Invoice.prototype.getInvoiceTotal = function() {
    return subtotal + this.getTaxAmount(this.subtotal);
};
除此之外,还可以使用为对象指定属性,如下所示:

Object.assign(Invoice.prototype, {
    getTaxAmount: function() {
        return (subtotal * this.taxRate);
    },
    getInvoiceTotal: function() {
        return subtotal + this.getTaxAmount(this.subtotal);
    }
});

请注意,使用后一个函数时,不要在函数末尾使用分号。

是的,这是一个语法错误。您更愿意使用赋值运算符,
=
,用于此类工作:

// The getTaxAmount method is added to the Invoice prototype
Invoice.prototype.getTaxAmount = function() {
    return (subtotal * this.taxRate);
};

// The getInvoiceTotal method is added to the Invoice prototype
Invoice.prototype.getInvoiceTotal = function() {
    return subtotal + this.getTaxAmount(this.subtotal);
};
除此之外,还可以使用为对象指定属性,如下所示:

Object.assign(Invoice.prototype, {
    getTaxAmount: function() {
        return (subtotal * this.taxRate);
    },
    getInvoiceTotal: function() {
        return subtotal + this.getTaxAmount(this.subtotal);
    }
});
请注意,在使用后一个函数时,在函数末尾不使用分号

所有JavaScript对象都从原型继承属性和方法

<script>
  var Invoice = function(subtotal, taxRate) {
  this.subtotal = subtotal;
  this.taxRate = taxRate;
};

Invoice.prototype.getTaxAmount = function() {
    return (subtotal * this.taxRate);
};

var myTax= new Invoice(10,5);
document.getElementById("demo").innerHTML =
"My tax amount is " + myTax.getTaxAmount (); 
</script>
JavaScript prototype属性还允许您添加新的方法对象构造函数:

ie:Date对象继承自Date.prototype。数组对象继承自Array.prototype。Person对象继承自Person.prototype

<script>
  var Invoice = function(subtotal, taxRate) {
  this.subtotal = subtotal;
  this.taxRate = taxRate;
};

Invoice.prototype.getTaxAmount = function() {
    return (subtotal * this.taxRate);
};

var myTax= new Invoice(10,5);
document.getElementById("demo").innerHTML =
"My tax amount is " + myTax.getTaxAmount (); 
</script>

var发票=功能(小计,税率){
这个。小计=小计;
该税率=税率;
};
Invoice.prototype.getTaxAmount=函数(){
报税表(小计*本税率);
};
var myTax=新发票(10,5);
document.getElementById(“demo”).innerHTML=
“我的税额为”+myTax.getTaxAmount();
所有JavaScript对象都从原型继承属性和方法

<script>
  var Invoice = function(subtotal, taxRate) {
  this.subtotal = subtotal;
  this.taxRate = taxRate;
};

Invoice.prototype.getTaxAmount = function() {
    return (subtotal * this.taxRate);
};

var myTax= new Invoice(10,5);
document.getElementById("demo").innerHTML =
"My tax amount is " + myTax.getTaxAmount (); 
</script>
JavaScript prototype属性还允许您添加新的方法对象构造函数:

ie:Date对象继承自Date.prototype。数组对象继承自Array.prototype。Person对象继承自Person.prototype

<script>
  var Invoice = function(subtotal, taxRate) {
  this.subtotal = subtotal;
  this.taxRate = taxRate;
};

Invoice.prototype.getTaxAmount = function() {
    return (subtotal * this.taxRate);
};

var myTax= new Invoice(10,5);
document.getElementById("demo").innerHTML =
"My tax amount is " + myTax.getTaxAmount (); 
</script>

var发票=功能(小计,税率){
这个。小计=小计;
该税率=税率;
};
Invoice.prototype.getTaxAmount=函数(){
报税表(小计*本税率);
};
var myTax=新发票(10,5);
document.getElementById(“demo”).innerHTML=
“我的税额为”+myTax.getTaxAmount();

太棒了!感谢您在这方面的快速帮助。这是我在书中发现的第一个错误,深度约500页:)计时器一响我就接受。@Adrian
subtotal
应该是
this。我想也是subtotal
。太棒了!感谢您在这方面的快速帮助。这是我在这本书中发现的第一个错误,深度约为500页:)我会在计时器停止时接受。@Adrian
subtotal
应该是
this。我想也是subtotal
。谢谢你在这方面的帮助。Jorg赢了你几分钟谢谢你的帮助。乔格以几分钟的优势击败了你。