JavaScript set()

0
7K

In JavaScript, a setter can be used to execute a function whenever a specified property is attempted to be changed. Setters are most often used in conjunction with getters to create a type of pseudo-property. It is not possible to simultaneously have a setter on a property that holds an actual value.

Examples

 

Defining a setter on new objects in object initializers

The following example define a pseudo-property current of object language. When current is assigned a value, it updates log with that value:

const language = {
  set current(name) {
    this.log.push(name);
  },
  log: [],
};

language.current = "EN";
console.log(language.log); // ['EN']

language.current = "FA";
console.log(language.log); // ['EN', 'FA']

Note that current is not defined, and any attempts to access it will result in undefined.

Search
Categories
Read More
Team Handball
Team Handball: The Fast-Paced Sport with Global Appeal
Team Handball: The Fast-Paced Sport with Global Appeal Team handball, often simply referred to...
By Leonard Pokrovski 2024-07-11 13:38:35 0 17K
Социальные проблемы
«V» значит Вендетта. V for Vendetta. (2005)
Альтернативное будущее. В Англии после страшной эпидемии неизвестного вируса повергшей страну в...
By Nikolai Pokryshkin 2023-03-02 11:12:13 0 37K
Спорт
Воин. Warrior. (2011)
Томми Конлон - молодой боец, вернувшийся домой после долгого отсутствия, чтобы приготовиться к...
By Nikolai Pokryshkin 2023-03-11 22:30:49 0 28K
Financial Services
Command-and-control regulation
Key points Command-and-control regulation sets specific limits for pollution...
By Mark Lorenzo 2023-07-21 19:23:46 0 11K
Business
What Are Common Barriers to Innovation, and How Can They Be Overcome?
Innovation is often hailed as the key to progress and growth in any organization or industry....
By Dacey Rankins 2025-03-20 14:58:44 0 4K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov