JavaScript set()

0
11K

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.

Zoeken
Categorieën
Read More
Marketing and Advertising
How Long Should an Outdoor Advertising Campaign Run?
One of the most common questions advertisers ask when planning an outdoor advertising campaign...
By Dacey Rankins 2026-02-02 18:12:13 0 528
Business
What Does a Growth Strategy Involve?
A business can only scale sustainably if it has a clear growth strategy. Growth strategies guide...
By Dacey Rankins 2025-09-04 13:26:05 0 3K
Science Fiction and Fantasy
The Dark Knight Rises. (2012)
Eight years after the Joker's reign of anarchy, Batman, with the help of the enigmatic Selina...
By Leonard Pokrovski 2022-12-26 19:02:55 0 22K
Bedrijvengids
The History of Games: From Ancient Play to Digital Masterpieces
Games have been an integral part of human culture for thousands of years. From the earliest forms...
By Dacey Rankins 2024-11-25 14:35:28 0 10K
Home Improvement
How to design a Room
Whether you don’t have the budget to hire an interior designer or simply enjoy the idea of...
By FWhoop Xelqua 2023-05-01 20:11:48 0 19K

BigMoney.VIP Powered by Hosting Pokrov