第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

我想聲明類中對(duì)象的值,并且僅在Im聲明新類時(shí)調(diào)用鍵。我該怎么辦?

我想聲明類中對(duì)象的值,并且僅在Im聲明新類時(shí)調(diào)用鍵。我該怎么辦?

UYOU 2022-09-23 16:16:19
這就是我到目前為止所做的。class PriceCalc {  constructor(Obj) {    this.parts = Object.keys(Obj);    this.cost =      "$" +      Object.values(Obj)        .reduce((a, b) => a + b, 0)        .toFixed(2);    this.retail =      "$" +      (        Object.values(Obj).reduce((a, b) => a + b, 0) +        Object.values(Obj).reduce((a, b) => a + b, 0) * 1.75      ).toFixed(2);    this.quote = qt + this.retail;  }}var total = new PriceCalc({  Motherboard: 520.99,  RAM: 250.4,  SSD: 500.8,  HDD: 400.66,  Case: 375.5,  Monitor: 600.75,  Keyboard: 100.99,  Mouse: 25.5});console.log(total.parts);console.log(total.cost);console.log(total.retail);console.log(total.quote);Id想要實(shí)現(xiàn)的是將值聲明在類中,然后只調(diào)用類似的東西var total = new PriceCalc({Motherboard, RAM, SSD, HDD});并返回指定部件的組合值,在本例中為 $1,672.85,對(duì)于此.cost
查看完整描述

1 回答

?
森林海

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊

  1. 將默認(rèn)值放入類屬性。

  2. 傳遞要獲取的屬性字符串。

  3. 用于獲取對(duì)象屬性this[b]

  4. 確保你把數(shù)字而不是字符串相加:會(huì)給你字符串串聯(lián)而不是算術(shù)運(yùn)算。qt + this.quote

class PriceCalc {

  Motherboard = 520.99;

  RAM = 250.40;

  SSD = 500.80;

  HDD = 400.66;

  Case = 375.50;

  Monitor = 600.75;

  Keyboard = 100.99;

  Mouse = 25.50;

  

  parts = []

  cost = '$0'

  retail = '$0'

  quote = '0'


  constructor(Obj) {

    this.parts = Obj.join(',');

    this.cost = "$" + (Obj.reduce((a, b) => a + this[b], 0).toFixed(2));

    this.retail = "$" + ((Obj.reduce((a, b) => a + this[b], 0) + (Obj.reduce((a, b) => a + this[b], 0) * 1.75)).toFixed(2));

    //this.quote = qt + this.retail;

  }

}


var total = new PriceCalc([

  'Motherboard',

  'RAM',

  'SSD',

  'HDD'

]);


console.log(total.parts)

console.log(total.cost)

console.log(total.retail)

console.log(total.quote)


查看完整回答
反對(duì) 回復(fù) 2022-09-23
  • 1 回答
  • 0 關(guān)注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)