The rand and generateId functions that generate a random identification number are proposed. Change them in any way - it is impossible.
The id property of the LoyaltyCard factory function should get the value from generateId , save it and make it read-only. The idea is that when accessing this property from the method of this.id , the identification number should not be re-generated.
How can I implement this when declaring properties?
//Неизменяемые функции: function rand(min, max) { return Math.ceil((max - min + 1) * Math.random()) + min - 1; } function generateId() { return Array(4).fill(1).map(value => rand(1000, 9999)).join('-'); } //Фабрика объектов: let LoyaltyCard = function(name, sum) { this.owner = name; this.id = generateId(); //так свойство генерирует новый ID при каждом обращении к нему this.balance = sum; this.discount = 0; this.orders = Array.of(sum); } LoyaltyCard.prototype.show = function() { console.log(`Карта ${this.id}:\nВладелец: ${this.owner}\nБаланс: ${this.balance} Q\nТекущая скидка: ${this.discount} %\nЗаказы:\n #1 на сумму ${this.orders[0]} Q\n #2 на сумму ${this.orders[1]} Q`); }
idfield, therefore, when trying to read theidfrom the object, nothing will be generated again - Grundy