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.

Cerca
Categorie
Leggi tutto
Social Issues
Capturing the Friedmans (2003)
Documentary on the Friedmans, a seemingly typical, upper-middle-class Jewish family whose world...
By Leonard Pokrovski 2023-07-31 20:28:12 0 530K
Genres
Art Genres
Genres and types of art include: Fine arts Painting (portrait, landscape, still life,...
By FWhoop Xelqua 2023-07-04 19:30:57 0 23K
Business
How Did the Leader Develop Their Leadership Style or Philosophy?
A leader’s style rarely appears overnight. It evolves over time, shaped by experiences,...
By Dacey Rankins 2025-08-14 21:36:00 0 3K
Business
Do Mentors or Coaches Need to Pre-Own the Expertise?
A common misconception in both mentoring and coaching is that the guide—whether a mentor or...
By Dacey Rankins 2025-07-23 12:13:47 0 4K
Money
What Are Utility Bills?
What Are Utility Bills? Utility bills are regular invoices that households and businesses...
By Leonard Pokrovski 2025-09-20 16:00:55 0 2K

BigMoney.VIP Powered by Hosting Pokrov