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
Conditions and Diseases
Understanding Conditions and Diseases: A Comprehensive Guide to Health
The human body is a complex system, and when something goes wrong, it can lead to a variety of...
Par Dacey Rankins 2024-11-06 16:57:54 0 10KB
Mental Health
ADHD: Medication - Stimulants
Methylphenidate and amphetamine or its derivatives are first-line treatments for ADHD as they are...
Par Kelsey Rodriguez 2023-04-28 18:05:20 0 12KB
Business
What are the Steps in the Customer Development Process?
The Customer Development process is a strategic framework used by entrepreneurs, startups, and...
Par Dacey Rankins 2025-03-25 15:36:40 0 11KB
Природа и наука
Лошадь Мечты. Dream Horse. (2021)
Устав от обыденной жизни, Джен решает ее изменить. Не имея ни опыта, ни средств, она убеждает...
Par Nikolai Pokryshkin 2022-09-07 19:02:28 0 30KB
Жизненные вопросы
В поисках Немо. Finding Nemo. (2003)
Рыба-клоун Марлин после трагической гибели жены в одиночку растит своего единственного сына Немо....
Par Nikolai Pokryshkin 2023-02-27 12:22:07 0 31KB

BigMoney.VIP Powered by Hosting Pokrov