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.

Site içinde arama yapın
Kategoriler
Read More
Социальные проблемы
Опасный пациент. The Shift. (2021)
Возвращаясь после ночной смены, врачи скорой помощи Изабель и Адамо получают срочный вызов. В...
By Nikolai Pokryshkin 2022-09-12 20:48:08 0 35K
Life Issues
Grave of the Fireflies. (1988)
The story of Seita and Setsuko, two young Japanese siblings, living in the declining days of...
By Leonard Pokrovski 2022-12-06 20:01:30 0 23K
Weight Loss
Weight Loss: Understanding the Journey to a Healthier You
Weight loss is a common goal for many people, whether for improving overall health, enhancing...
By Dacey Rankins 2024-11-21 16:15:13 0 11K
Physics
Unraveling the Mysteries of the Universe: A Journey through the World of Physics
Unraveling the Mysteries of the Universe: A Journey through the World of Physics Physics, often...
By Leonard Pokrovski 2024-05-25 11:04:07 0 26K
Business
How Do I Identify My Ideal Customer Profile (ICP)?
One of the biggest reasons startups fail at sales is not because their pitch is weak, or their...
By Dacey Rankins 2025-12-08 18:41:04 0 954

BigMoney.VIP Powered by Hosting Pokrov