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
Научная фантастика и фэнтези
Терминатор 2: Судный день. Terminator 2: Judgment Day. (1991)
Прошло более десяти лет с тех пор, как киборг из 2029 года пытался уничтожить Сару Коннор —...
Par Nikolai Pokryshkin 2022-11-22 16:19:09 0 24KB
Consumer Information
Understanding Home Consumer Information: A Guide for Homeowners
Navigating the world of homeownership can be overwhelming, especially with the vast amount of...
Par Dacey Rankins 2024-10-29 15:51:00 0 14KB
Business
How Does Technology, Particularly AI, Influence Startup Innovation?
In today’s fast-evolving business landscape, technology, especially artificial intelligence...
Par Dacey Rankins 2025-03-21 14:15:44 0 14KB
Religion and spirituality
The Only Video You Need to Start Manipulating Energy RIGHT NOW! (full guide)
In the video titled "The Only Video You Need to Start Manipulating Energy," published on February...
Par Nikolai Pokrovski 2025-02-15 22:29:07 0 17KB
Mental Health
ADHD: SOCIETY
The youngest children in a class have been found to be more likely to be diagnosed as having...
Par Kelsey Rodriguez 2023-04-07 18:27:38 0 12KB

BigMoney.VIP Powered by Hosting Pokrov