JavaScript set()

0
7K

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.

Căutare
Categorii
Citeste mai mult
Conventions
The World of Game Conventions: Celebrating Gaming Culture and Community
Game conventions are the epicenter of gaming culture, offering enthusiasts a chance to gather,...
By Dacey Rankins 2024-12-02 14:55:38 0 6K
Information Services
Company Information Security
From cybersecurity solutions to risk management systems, these services are tailored to meet the...
By Michael Pokrovski 2024-03-13 22:07:40 0 23K
Television
PLAYTV by Brightcove.
Learn how to navigate all types of communication with PLAYTV by Brightcove. Featuring a curated...
By Nikolai Pokryshkin 2023-05-13 12:52:08 0 30K
Senior Health
Health Features of an Elderly Person
Health Features of an Elderly Person   The health of mature and elderly people is...
By Leonard Pokrovski 2024-04-22 21:41:55 0 12K
Business
Tools for analytics and sales management on marketplaces
In recent years, marketplaces have become an important part of sales strategies for many...
By Dacey Rankins 2024-09-13 19:20:46 0 10K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov