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.

Buscar
Categorías
Read More
Спорт
Дангал. Dangal. (2016)
История спортсмена Махавира Сингха Пхогата, который по причине бедности был вынужден бросить...
By Nikolai Pokryshkin 2023-02-01 23:47:22 0 31K
Business
How does the CEO handle failure? Insights into the CEO's approach to setbacks can offer valuable lessons in resilience and adaptability.
In the high-stakes world of corporate leadership, failure is inevitable. Even the most successful...
By Dacey Rankins 2025-07-01 19:51:43 0 5K
Financial Services
How the AD/AS model incorporates growth, unemployment, and inflation
Key points The aggregate demand/aggregate supply, or AD/AS, model is one of the...
By Mark Lorenzo 2023-03-31 19:57:02 0 14K
Juegos
Games labeled "PS5 Pro Enhanced" will offer a stable 60 FPS, increased resolution, or improved ray tracing
Games labeled "PS5 Pro Enhanced" will offer a stable 60 FPS, increased resolution, or improved...
By Leonard Pokrovski 2024-04-01 20:57:51 0 22K
Programming
Python __init__ Method
The __init__ method is the Python equivalent of the C++ constructor in an...
By Jesse Thomas 2023-03-17 20:19:20 0 11K

BigMoney.VIP Powered by Hosting Pokrov