JavaScript set()

0
12χλμ.

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.

Αναζήτηση
Κατηγορίες
Διαβάζω περισσότερα
Business
How Often Should I Update My Business Biography?
A business biography serves as a personal and professional narrative that highlights the key...
από Dacey Rankins 2025-01-06 15:55:28 0 16χλμ.
Animal
The Importance of Animal Health: Ensuring Well-being for All Species
Animal health is a vital aspect of maintaining a balanced ecosystem and ensuring the well-being...
από Dacey Rankins 2025-01-24 18:32:06 0 15χλμ.
История
Иди и смотри. (1985)
1943 год, Великая Отечественная война. Подросток Флёра, откопав карабин среди обрывков колючей...
από Nikolai Pokryshkin 2023-01-12 19:19:09 0 31χλμ.
Социальные проблемы
Бешеные псы. Reservoir Dogs. (1992)
Это должно было стать идеальным преступлением. Задумав ограбить ювелирный магазин, криминальный...
από Nikolai Pokryshkin 2023-01-11 17:57:32 0 33χλμ.
Marketing and Advertising
How Do I Start a PPC Campaign?
Starting a pay-per-click (PPC) campaign can feel overwhelming, especially for beginners. With...
από Dacey Rankins 2026-02-04 17:40:15 0 4χλμ.

BigMoney.VIP Powered by Hosting Pokrov