Как добавить значение в функцию конструктор, а не просто перезаписать.
Привет.
Помогите понять как сделать так, чтобы когда я добавляю значение в art.add и затем вывожу art.list оно не выводило только последнее значение которое я записал, а выводило все значения которые я записывал. Спасибо. Вот мой код: var arr = []; class Art { constructor() { } add(date, amount, currency, product) { this.date = date; this.amount = amount; this.currency = currency; this.product = product; } list() { return arr = [this.date, this.amount, this.currency, this.product]; console.log(arr); } clear(date) { arr.splice(1,1); console.log(this.list()); } total() { return this.amount + this.currency; } } const art = new Art(); art.add('2017-04-25', 2, 'USD', 'Jogurt'); art.add('2017-03-13', 5, 'EUR', 'Milk'); console.log(art.list()); console.log(art.total()); |
https://developer.mozilla.org/ru/doc...cts/Array/push
var arr = []; class Art { constructor() { } add(date, amount, currency, product) { this.date = date; this.amount = amount; this.currency = currency; this.product = product; arr.push([date, amount, currency, product]); } list() { return arr; console.log(arr); } clear(date) { arr.splice(1,1); console.log(this.list()); } total() { return this.amount + this.currency; } } const art = new Art(); art.add('2017-04-25', 2, 'USD', 'Jogurt'); art.add('2017-03-13', 5, 'EUR', 'Milk'); console.log(art.list()); console.log(art.total()); |
Часовой пояс GMT +3, время: 21:58. |