JavaScript set()

0
11KB

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.

Rechercher
Catégories
Lire la suite
Financial Services
Macroeconomics
If you want to sum up what economics means, you could do so with the following statement:...
Par Mark Lorenzo 2023-01-24 20:07:42 0 17KB
Business
How Can a Solopreneur Build a Strong Personal Brand?
In today’s competitive marketplace, building a strong personal brand is crucial for...
Par Dacey Rankins 2025-02-12 15:41:41 0 16KB
Tennis
Tennis: The Timeless Sport of Skill, Strategy, and Endurance
Tennis: The Timeless Sport of Skill, Strategy, and Endurance Tennis, a sport beloved for its...
Par Leonard Pokrovski 2024-07-11 13:43:40 0 26KB
Dice
The Fascinating World of Dice Games: A Blend of Chance and Strategy
Dice are among the oldest and most universal gaming tools, used in countless games across...
Par Dacey Rankins 2024-12-02 14:52:55 0 9KB
Социальные проблемы
Мадам Клод. Madame Claude. (2021)
Париж, 1960-е. Мадам Клод возглавляет процветающий бизнес, связанный с проституцией, который дает...
Par Nikolai Pokryshkin 2022-09-12 22:15:57 0 30KB

BigMoney.VIP Powered by Hosting Pokrov