JavaScript "Object Method"

JavaScript "Object Method"

本文关键字:quot Method Object JavaScript      更新时间:2024-03-31
  function Money(bill, accu, hb, food, webHosting, lynda) {
  Money.bill = bill;
  Money.accu = accu;
  Money.hb = hb;
  Money.food = food;
  Money.webHosting = webHosting;
  Money.lynda = lynda;
  Money.total = function () {
  (Money.bill + Money.accu + Money.hb + Money.food + Money.webHosting +    Money.lynda)
    return Money.total;
  };
}
var cost = new Money(2500, 5000, 2000, 6000, 1000, 30000);
 Money.total();

我已经在最后使用变量为对象定义了所有内容。当我运行 Money.total("money"对象方法(时,它返回为"函数"请帮忙。我想要所有东西的总和。

您需要

使用this而不是"Money"来读取/写入新创建对象的属性:

function Money(bill, accu, hb, food, webHosting, lynda) {
  this.bill = bill;
  this.accu = accu;
  this.hb = hb;
  this.food = food;
  this.webHosting = webHosting;
  this.lynda = lynda;
  this.total = function() {
      return this.bill + this.accu + this.hb + this.food + this.webHosting + this.lynda;
  }
}

目前还不清楚你想用你的"整体"方法做什么。 如果你能描述它应该做什么,那么我们可以帮助实现,但我提供了一个对属性求和的实现。