JavaScript set()

0
12K

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.

Site içinde arama yapın
Kategoriler
Read More
Productivity
How to stop procrastinating at work?
How to Stop Procrastinating at Work Procrastination at work is rarely about laziness or lack of...
By Michael Pokrovski 2026-04-27 20:42:26 0 1K
Marketing and Advertising
What Are Examples of Outdoor Advertising?
Outdoor advertising surrounds people every day—on highways, sidewalks, public transit, and...
By Dacey Rankins 2026-01-30 19:16:33 0 5K
Marketing and Advertising
How Does Affiliate Marketing Work?
Affiliate marketing is one of the most widely adopted digital marketing strategies worldwide....
By Dacey Rankins 2025-09-18 16:01:09 0 10K
Financial Services
Interpreting the aggregate demand/aggregate supply model
Key points The aggregate demand/aggregate supply model is a model that...
By Mark Lorenzo 2023-03-08 17:45:51 0 24K
Business
What Are Some Good Closing Statements or Phrases?
Effective closing statements and phrases can significantly improve your success rate when...
By Dacey Rankins 2025-11-19 20:14:26 0 7K

BigMoney.VIP Powered by Hosting Pokrov