JavaScript set()

0
6KB

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.

Rechercher
Catégories
Lire la suite
Ужасы
Психо. Psycho. (1960)
Мэрион Крэйн недовольна романом с разведенным мужчиной, который уделяет ей внимание только...
Par Nikolai Pokryshkin 2022-11-24 20:52:18 0 19KB
Спорт
Дом на глубине. The Deep House. (2021)
Видеоблогеры Тина и Бен путешествуют по миру, снимая старые заброшенные здания. Решив впечатлить...
Par Nikolai Pokryshkin 2022-09-03 20:34:06 0 27KB
Homemaking
Top 5 Home Based Business Ideas
The opening of home productions is gaining more and more popularity. A small workshop is a good...
Par FWhoop Xelqua 2023-03-09 19:28:57 0 12KB
Financial Services
Explicit and implicit costs and accounting and economic profit
Key points Privately owned firms are motivated to earn profits. Profit is the...
Par Mark Lorenzo 2023-07-17 19:32:29 0 8KB
Television
CBS, WIVB 4 Live TV. Buffalo, New York, United States.
WIVB-TV, (UHF digital channel 39), is a CBS-affiliated television station located in Buffalo, New...
Par Nikolai Pokryshkin 2022-11-03 10:26:05 0 31KB
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov