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.

Cerca
Categorie
Leggi tutto
Financial Services
The economics of pollution
Key Points Economic production can cause environmental damage. This tradeoff...
By Mark Lorenzo 2023-07-20 18:29:02 0 16K
Altre informazioni
Baraka (1992)
A collection of expertly photographed scenes of human life and religion. My Link
By Leonard Pokrovski 2023-07-05 20:24:56 0 35K
Business
What are the risks associated with passive income streams?
Passive income is often hailed as the ultimate way to achieve financial freedom, allowing...
By Dacey Rankins 2025-01-16 15:37:26 0 13K
Biotechnology and Pharmaceuticals
Biotechnology: Unlocking the Future of Medicine, Agriculture, and Sustainability
Biotechnology, a field at the intersection of biology and technology, has revolutionized how we...
By Dacey Rankins 2024-11-12 14:34:17 0 13K
Business
Youtube Video Promotion: 7 Proven Ways
In the era of digital technology and social media, promoting video content is becoming one of the...
By Dacey Rankins 2024-09-19 16:49:03 0 16K

BigMoney.VIP Powered by Hosting Pokrov