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
Искусство, культура и развлечения
Никто не знает, что я здесь. Nobody Knows I'm Here. (2020)
Мемо Гарридо живет в уединении на чилийской овцеводческой ферме. В прошлом он известный певец,...
Par Nikolai Pokryshkin 2022-10-01 21:08:16 0 26KB
Marketing and Advertising
What Advertising Biographies Are Used in Business Schools?
Business schools do not teach advertising solely through frameworks, metrics, and case studies....
Par Dacey Rankins 2026-01-08 12:36:25 0 3KB
Mental Health
Dyslexia: History
Dyslexia was clinically described by Oswald Berkhan in 1881, but the term dyslexia was coined in...
Par Kelsey Rodriguez 2023-07-07 17:13:04 0 11KB
Tile Games
Tile Games: The Fascinating World of Strategy and Fun
Tile games have captivated players of all ages for centuries, combining elements of strategy,...
Par Dacey Rankins 2024-11-20 18:30:58 0 11KB
Business
How Do I Memorize a Speech?
Memorizing a speech is one of the most common challenges people face in public...
Par Dacey Rankins 2025-12-11 16:13:14 0 3KB

BigMoney.VIP Powered by Hosting Pokrov