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
Business
Top 3 Mistakes in Presenting Your Product
Let's figure out what not to do to find and get your business angel. Let's list the most common...
By Dacey Rankins 2024-09-13 14:21:50 0 13K
Money
What is a balloon payment?
What is a balloon payment? A balloon payment is a large, lump-sum payment due at the end of...
By Leonard Pokrovski 2025-09-27 16:48:35 0 3K
Animation
Top 10 Greatest Anime
Anime, a wondrous and captivating form of storytelling, has its roots in the land of the rising...
By FWhoop Xelqua 2023-04-27 17:44:48 0 18K
Gymnastics
The Graceful Artistry of Gymnastics: A Sport of Power, Precision, and Poise
The Graceful Artistry of Gymnastics: A Sport of Power, Precision, and Poise Gymnastics, often...
By Leonard Pokrovski 2024-06-27 18:32:34 0 21K
Business
How to Ensure Stories Are Testable in Agile Development
In Agile software development, user stories form the foundation of work planning and execution. A...
By Dacey Rankins 2025-08-26 11:51:33 0 6K

BigMoney.VIP Powered by Hosting Pokrov