JavaScript set()

0
11KB

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.

Rechercher
Catégories
Lire la suite
Business
What Are Common Mistakes to Avoid When Writing a Business Biography?
Writing a business biography can be a rewarding but challenging task. It requires careful...
Par Dacey Rankins 2025-01-06 15:56:44 0 12KB
Farms
Which animals can be bred at home for income
Which animals can be bred at home for income Hello world! What animals can be bred at home...
Par Leonard Pokrovski 2024-04-25 12:08:53 0 16KB
Business
How Much Does It Cost / What Budget Do I Need for Business Development Activities?
Business development (BD) is widely recognized as a critical driver of growth for companies of...
Par Dacey Rankins 2025-11-17 18:49:44 0 1KB
Programming
Python Random
If you ever want to make some random choice or number for something, there's a pre-installed...
Par Jesse Thomas 2023-02-13 21:54:59 0 11KB
Programming
Make A 2D Game With Unity
How to Create a 2D Game in Unity To create 2D games, we need to use Unity Hub. Unity...
Par Jesse Thomas 2023-06-28 21:18:55 0 13KB

BigMoney.VIP Powered by Hosting Pokrov