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
Personal Finance
What Investments Have Tax Advantages?
What Investments Have Tax Advantages? Taxes play a major role in determining how much of your...
Par Leonard Pokrovski 2025-12-19 23:15:39 0 2KB
Business
How Does Viral Growth Work or What Drives It?
Viral growth is often described as the “holy grail” of scaling businesses. Instead of...
Par Dacey Rankins 2025-09-17 10:45:24 0 4KB
Financial Services
The collapse of Silicon Valley bank
The collapse of Silicon Valley Bank: how and why the main bank of techno-startups of Silicon...
Par FWhoop Xelqua 2023-03-15 15:48:02 0 18KB
Business
What Legacy Did They Leave Behind?
A leader’s true impact is often measured not during their lifetime, but in the legacy they...
Par Dacey Rankins 2025-06-27 16:41:31 0 4KB
Business
How Do I Set Up Google Analytics on My Website?
Setting up Google Analytics is one of the most valuable steps you can take to understand your...
Par Dacey Rankins 2025-08-30 00:42:37 0 9KB

BigMoney.VIP Powered by Hosting Pokrov