JavaScript set()

0
8كيلو بايت

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.

البحث
الأقسام
إقرأ المزيد
Спорт
Дангал. Dangal. (2016)
История спортсмена Махавира Сингха Пхогата, который по причине бедности был вынужден бросить...
بواسطة Nikolai Pokryshkin 2023-02-01 23:47:22 0 26كيلو بايت
Научная фантастика и фэнтези
Райя и последний дракон. Raya and the Last Dragon. (2021)
Давным-давно в волшебной стране Кумандре бок о бок с людьми жили драконы — создатели и...
بواسطة Nikolai Pokryshkin 2022-09-21 18:57:43 0 23كيلو بايت
Financial Services
Using a person's budget to understand the balance of payments
To get a better understanding of the balance of payments accounts, let’s think of a...
بواسطة Mark Lorenzo 2023-03-09 18:06:55 0 10كيلو بايت
Business
What is corporate culture and why do organizations need it?
What is corporate culture and why do organizations need it? The concept of corporate...
بواسطة Leonard Pokrovski 2024-08-13 22:16:13 0 13كيلو بايت
Ask an Expert
Questions for Experts
What questions can be posed to the expert?Over the years of our company's work, we have known...
بواسطة FWhoop Xelqua 2023-01-10 15:38:39 0 17كيلو بايت

BigMoney.VIP Powered by Hosting Pokrov