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
Mental Health
Dementia: Vascular
Vascular dementia accounts for at least 20% of dementia cases, making it the second most common...
By Kelsey Rodriguez 2023-07-26 19:30:19 0 18K
Satire
Top 16 best TV series of 2022
Paradoxically, this terrible war year was very rich in good TV shows. I picked you eight of the...
By FWhoop Xelqua 2023-01-02 17:51:46 0 26K
Business
How Do You Conduct Facility Audits?
Facility audits are systematic evaluations of a building’s operations, systems, safety...
By Dacey Rankins 2026-04-16 20:23:36 0 2K
Business
How to maintain compliance in office operations?
It was already approved. Or at least, that’s what everyone believed. The document had...
By Dacey Rankins 2026-05-01 17:07:19 0 907
Human Resources
Is Offshoring Still Relevant in 2026?
Over the past few decades, offshoring has been a cornerstone of globalization, enabling companies...
By Dacey Rankins 2026-04-01 21:40:32 0 2K

BigMoney.VIP Powered by Hosting Pokrov