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.

Căutare
Categorii
Citeste mai mult
Economics
How Does Capitalism Affect Workers?
How Does Capitalism Affect Workers? Capitalism, the economic system where private individuals or...
By Leonard Pokrovski 2026-02-12 22:12:27 0 8K
Business
What Should I Look for When Hiring a Business Consultant?
Hiring a business consultant can be one of the best decisions a company makes, especially when...
By Dacey Rankins 2025-02-11 23:20:31 0 8K
Business
Can I Use Images From the Internet Legally?
The image was perfect. It captured exactly the mood the company wanted. The colors worked. The...
By Dacey Rankins 2026-06-11 12:31:42 0 2K
Economics
What is renewable energy economics?
What Is Renewable Energy Economics? There is a moment, usually just before dawn, when a wind...
By Leonard Pokrovski 2026-05-22 21:37:31 0 5K
Card Games
Collectible Card Game
Collectible Card Game A Collectible Card Game (CCG) is a type...
By Leonard Pokrovski 2024-04-10 00:52:26 0 14K

BigMoney.VIP Powered by Hosting Pokrov