JavaScript set()

0
11K

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
Subcultures
Exploring Society's Subcultures: Unveiling the Tapestry of Diversity
Society is a kaleidoscope of human experiences, where individuals come together, each bringing...
By Dacey Rankins 2024-06-13 16:22:46 0 18K
Социальные проблемы
Старикам тут не место. No Country for Old Men. (2007)
Обычный работяга обнаруживает в пустыне гору трупов, набитый героином грузовик и соблазнительную...
By Nikolai Pokryshkin 2023-02-22 18:21:10 0 22K
История
Взвод. Platoon. (1986)
В сентябре 1967 года куда-то в приграничный район между Вьетнамом и Камбоджей прибыл рядовой...
By Nikolai Pokryshkin 2023-04-16 17:55:42 0 32K
Libraries
The Timeless Haven: Celebrating Libraries in the Digital Age
In an era where information seems to flow endlessly through digital channels, the significance of...
By Dacey Rankins 2024-05-20 14:35:06 0 18K
Financial Services
The economics of pollution
Key Points Economic production can cause environmental damage. This tradeoff...
By Mark Lorenzo 2023-01-31 15:07:38 0 13K

BigMoney.VIP Powered by Hosting Pokrov