1 回答

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊
將默認(rèn)值放入類屬性。
傳遞要獲取的屬性字符串。
用于獲取對(duì)象屬性
this[b]
確保你把數(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)
添加回答
舉報(bào)