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
Business
How Do I Find the Right Mentor or Coach?
Finding the right mentor or coach can be a transformative step in your personal or professional...
By Dacey Rankins 2025-07-09 14:22:21 0 6K
Clothing
10 Tips for Successfully Buying Clothes Online
More than 50% of purchases are now made online. This is beneficial for everyone: shop owners may...
By FWhoop Xelqua 2022-09-22 13:56:43 0 46K
Altre informazioni
Baraka (1992)
A collection of expertly photographed scenes of human life and religion. My Link
By Leonard Pokrovski 2023-07-05 20:24:56 0 35K
Business
Are There Examples of Well-Crafted Startup Biographies?
Many of the world’s most successful companies have fascinating origin stories that not only...
By Dacey Rankins 2025-04-19 14:43:11 0 11K
Business
How Do I Register a Business?
Registering a business is a vital step in turning your idea into a legitimate, operating entity....
By Dacey Rankins 2025-04-30 12:49:49 0 10K

BigMoney.VIP Powered by Hosting Pokrov