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 Is a Good Retention Rate or Benchmark?
When companies measure user retention, one of the first questions leadership asks is: “Is...
Par Dacey Rankins 2025-09-16 16:29:27 0 6KB
Business
What Challenges Do Intrapreneurs Commonly Face?
Intrapreneurship is an exciting and impactful approach to driving innovation from within a...
Par Dacey Rankins 2025-04-17 12:52:38 0 11KB
Marketing and Advertising
How to Distribute a Press Release (and Actually Get Media Coverage)
Introduction: Writing Is Half the Battle — Distribution Wins the War You’ve written...
Par Dacey Rankins 2025-10-28 17:09:26 0 3KB
Marketing and Advertising
What Are the Best Affiliate Programs or Networks to Join?
Affiliate marketing success often depends on the quality of the programs you join. The right...
Par Dacey Rankins 2025-09-18 16:16:59 0 6KB
Business
What Are Segments?
In Google Analytics, raw data can sometimes be overwhelming. Looking at total users, sessions, or...
Par Dacey Rankins 2025-08-30 00:51:53 0 8KB

BigMoney.VIP Powered by Hosting Pokrov