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`); } 
  • Not very clear. What does the idea mean when accessing this property from the method of this.id. What method do you mean? - Stepan Kasyanenko
  • Added to the code, so it should be clearer. - Oleg Barbasov
  • // so the property generates a new ID each time it is accessed - the function returns a string that is written in the id field, therefore, when trying to read the id from the object, nothing will be generated again - Grundy
  • @Grundy is correct. - Stepan Kasyanenko
  • With each call, the value will be new. const card = new LoyaltyCard ('Ivan Ivanov', 6300); console.log (card); - Oleg Barbasov

1 answer 1

If I vavs correctly understood:

 //Неизменяемые функции: 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('-'); } const LoyaltyCard = function(name, sum) { // Значение хранится в св-ве функции. if(LoyaltyCard.commonId_ === undefined) { LoyaltyCard.commonId_ = generateId(); } this.owner = name; this.id = LoyaltyCard.commonId_; // Теперь генерируется только при самом первом вызове. this.balance = sum; this.discount = 0; this.orders = Array.of(sum); } const a = new LoyaltyCard('a', 124); const b = new LoyaltyCard('b', 124); const c = new LoyaltyCard('c', 124); console.log(a.id, b.id, c.id); 

  • and you could just make a call to the generateId function for the constructor and call it once, and assign the result, then the condition is not needed - Grundy
  • Well, you can and so) - ThisMan
  • It doesn't seem to work. The code returns new values ​​each time it is executed. repl.it/@Heithwald/SteepMajesticEngineering - Oleg Barbasov
  • @OlegBarbasov did you run my code? He displays the same value - ThisMan
  • @OlegBarbasov every time you click Run , you can say you reload the browser, so the values ​​are different. On one page, all instances will have the same id . See 3 identical numbers - ThisMan