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
Business
What Are the Benefits of Intrapreneurship for Organizations?
In a rapidly evolving business landscape, companies must consistently innovate to stay relevant...
By Dacey Rankins 2025-04-17 12:49:09 0 11K
Business
How Is User Behavior in Search Different From General Behavior on Websites?
When people browse the internet, their actions can be categorized into two broad contexts:...
By Dacey Rankins 2025-08-25 17:13:33 0 8K
Business
Data Fabric
Data Fabric The concept of Data Fabric and the main elements of this concept It is an...
By Leonard Pokrovski 2024-08-26 21:59:54 0 20K
Rehber
The Incredible Story of Board Games
The oldest "tabletops" appeared more than five thousand years ago and passed an interesting path...
By FWhoop Xelqua 2022-11-27 13:40:03 0 26K
Business and Corporate Finance
What Is the Difference Between a CFO and a CEO?
  What Is the Difference Between a CFO and a CEO? In any successful organization,...
By Leonard Pokrovski 2026-01-11 05:51:25 0 3K

BigMoney.VIP Powered by Hosting Pokrov