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.

Pesquisar
Categorias
Leia Mais
Социальные проблемы
Удалить историю. Delete History. (2020)
Жизнь троих обитателей городка подчинена новейшим технологиям и опутана социальными сетями....
Por Nikolai Pokryshkin 2022-10-15 21:52:58 1 26K
Programming
Python ValueError
Python ValueError is raised when a function receives an argument of the correct type, but an...
Por Jesse Thomas 2023-03-27 22:06:51 0 11K
Business
What Were the Major Challenges They Faced, and How Did They Overcome Them?
Every successful leader has a story shaped not just by victories, but by the challenges they...
Por Dacey Rankins 2025-06-26 16:57:18 0 5K
Спорт
Дом на глубине. The Deep House. (2021)
Видеоблогеры Тина и Бен путешествуют по миру, снимая старые заброшенные здания. Решив впечатлить...
Por Nikolai Pokryshkin 2022-09-03 20:34:06 0 37K
Научная фантастика и фэнтези
Космическая одиссея 2001 года. 2001: A Space Odyssey. (1968)
Экипаж космического корабля «Дискавери» — капитаны Дэйв Боумэн, Фрэнк Пул и их...
Por Nikolai Pokryshkin 2023-01-10 11:12:11 0 34K

BigMoney.VIP Powered by Hosting Pokrov