JavaScript set()

0
11Кб

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.

Поиск
Категории
Больше
Systems
Computers - How they have come to be, and what they are
Computer (English computer, IPA: [kəmˈpjuː.tə(ɹ)]- "calculator", from the Latin computare - to...
От FWhoop Xelqua 2023-02-16 15:33:57 0 22Кб
Business
How Do CEOs Make Decisions in High-Pressure or Uncertain Situations?
CEOs often face decision-making moments under extreme pressure and uncertainty—whether...
От Dacey Rankins 2025-08-07 17:25:57 0 8Кб
Tobacco
The Dangers of Smoking
On the dangers of smoking Smoking is one of the most significant threats to human...
От FWhoop Xelqua 2022-10-18 13:15:27 0 23Кб
Marketing and Advertising
Why Digital Marketing Is Important
Introduction: The Shift to Digital The way people shop, communicate, and consume information has...
От Dacey Rankins 2025-10-01 14:46:56 0 3Кб
Marketing and Advertising
What Kind of Content Should I Post to Engage People on Social Media?
In today’s digital landscape, social media content is the lifeblood of audience engagement....
От Dacey Rankins 2025-11-03 14:57:06 0 4Кб

BigMoney.VIP Powered by Hosting Pokrov