JavaScript set()

0
9KB

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
Ужасы
Воспоминания об убийстве. Memories of Murder. (2003)
В 1986 году в провинциальном городе Хвасон две местные женщины были убиты одним и тем же...
Par Nikolai Pokryshkin 2023-04-02 20:10:40 0 40KB
Научная фантастика и фэнтези
Ая и ведьма. Earwig and the Witch. (2021)
Несмотря на то что 10-летняя Ая не знает родительской ласки и воспитывается в детском приюте...
Par Nikolai Pokryshkin 2022-09-21 19:57:32 0 33KB
Real Estate
How to choose an apartment? Basic tips when buying a home
The question of how to choose an apartment can seem too complicated – especially if you...
Par FWhoop Xelqua 2022-09-26 10:37:51 0 26KB
Искусство, культура и развлечения
Звук металла. Sound of Metal. (2020)
Blackgammon – так называется панк-металическая группа, сколоченная барабанщиком Рубеном и...
Par Nikolai Pokryshkin 2022-09-25 20:31:20 0 32KB
Environment
Hammerhead Sharks
The hammerhead sharks are a group of sharks that form the family Sphyrnidae, so named for the...
Par FWhoop Xelqua 2023-04-25 18:37:58 0 16KB

BigMoney.VIP Powered by Hosting Pokrov