JavaScript set()

0
11K

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
kişiler
"Traitor to the motherland and draft dodger": how the boxing legend from the United States did not want to participate in the mobilization
A high-profile story about Muhammad Ali. In the middle of the last century, the US military...
By FWhoop Xelqua 2022-11-16 17:50:51 0 44K
Nursing
Nursing
Nursing Patient care is a set of measures to serve patients (limited in...
By Leonard Pokrovski 2024-04-17 18:33:52 0 18K
Horror
The Unholy (2021)
A hearing-impaired girl is visited by the Virgin Mary and can suddenly hear, speak, and heal the...
By Leonard Pokrovski 2022-09-23 19:55:53 0 33K
Hardware
The Evolution of Hardware: The Backbone of Modern Technology
In the world of technology, hardware refers to the physical components of a computer or...
By Dacey Rankins 2024-11-05 15:49:11 0 10K
Business
What Are the Company's Core Values, and How Do They Impact the Workplace?
Why Understanding Core Values Is Key to Cultural Alignment and Operational Integrity When...
By Dacey Rankins 2025-06-10 15:26:52 0 8K

BigMoney.VIP Powered by Hosting Pokrov