我想对数组price的所有属性this.hamburgers进行汇总,以找到它们的总金额,并在将来找到要转移到localStorage的订单的平均成本。

但是,当我尝试在方法price中获取sumHamburgers()的所有属性时,得到的结果是:“未定义”。



class Magazine {
  constructor() {
    this.hamburgers = [];
  }

  sumHamburgers() {
    return this.hamburgers.map(el => el.price).reduce((p, c) => p + c);
  }

  hamburgersAvgN() {
    return this.sumHamburgers() / Hamburger.counter;
  }

  toStorage() {
    let serialCounter = JSON.stringify(Hamburger.counter);
    let serialAvgN = JSON.stringify(this.hamburgersAvgN());
    return localStorage.setItem(['orders', 'avg'], [`number of orders: ${serialCounter}`, ` average check: ${serialAvgN} UAH`]);
  }
}

class Hamburger {
  constructor(size, ...stuffing) {
    this.size = size;
    this.stuffing = stuffing;
    this.toppingArr = [];
    this.counter = ++Hamburger.counter;
  }

  calculatePrice() {
    const stuffingCost = this.stuffing.map(el => el.price).reduce((p, c) => p + c);
    let allCost;
    if (this.toppingArr.length === 0) {
      return allCost = this.size.price + stuffingCost;
    } else {
      const toppingCost = this.toppingArr.map(el => el.price).reduce((p, c) => p + c);
      return allCost = this.size.price + stuffingCost + toppingCost;
    }
  }
}

Hamburger.counter = 0;

Hamburger.small = {
  name: 'small',
  price: 10,
  kcal: 200
}

Hamburger.cheese = {
  name: 'cheese',
  price: 4,
  kcal: 10
}

Hamburger.meet = {
  name: ' meet',
  price: 40,
  kcal: 103
}

let magazine = new Magazine;
magazine.hamburgers.push(new Hamburger(Hamburger.small, Hamburger.meet));
magazine.hamburgers.push(new Hamburger(Hamburger.small, Hamburger.cheese));
magazine.hamburgers.push(new Hamburger(Hamburger.small, Hamburger.meet));

console.log(magazine.sumHamburgers());

最佳答案

您要调用el.caculatePrice()而不是不存在的el.price



class Magazine {
  constructor() {
    this.hamburgers = [];
  }

  sumHamburgers() {
    // My fix is here: `el.calculatePrice()` instead of `el.price`
    return this.hamburgers.map(el => el.calculatePrice()).reduce((p, c) => p + c);
  }

  hamburgersAvgN() {
    return this.sumHamburgers() / Hamburger.counter;
  }

  toStorage() {
    let serialCounter = JSON.stringify(Hamburger.counter);
    let serialAvgN = JSON.stringify(this.hamburgersAvgN());
    return localStorage.setItem(['orders', 'avg'], [`number of orders: ${serialCounter}`, ` average check: ${serialAvgN} UAH`]);
  }
}

class Hamburger {
  constructor(size, ...stuffing) {
    this.size = size;
    this.stuffing = stuffing;
    this.toppingArr = [];
    this.counter = ++Hamburger.counter;
  }

  calculatePrice() {
    const stuffingCost = this.stuffing.map(el => el.price).reduce((p, c) => p + c);
    let allCost;
    if (this.toppingArr.length === 0) {
      return allCost = this.size.price + stuffingCost;
    } else {
      const toppingCost = this.toppingArr.map(el => el.price).reduce((p, c) => p + c);
      return allCost = this.size.price + stuffingCost + toppingCost;
    }
  }
}

Hamburger.counter = 0;

Hamburger.small = {
  name: 'small',
  price: 10,
  kcal: 200
}

Hamburger.cheese = {
  name: 'cheese',
  price: 4,
  kcal: 10
}

Hamburger.meet = {
  name: 'meet',
  price: 40,
  kcal: 103
}

let magazine = new Magazine;
magazine.hamburgers.push(new Hamburger(Hamburger.small, Hamburger.meet));
magazine.hamburgers.push(new Hamburger(Hamburger.small, Hamburger.cheese));
magazine.hamburgers.push(new Hamburger(Hamburger.small, Hamburger.meet));

console.log(magazine.sumHamburgers());

关于javascript - 为什么不能总结数组的所有属性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52607663/

10-12 07:22