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
Productivity
What is flow state and how do I achieve it?
What Is Flow State and How Do I Achieve It? Flow state is a psychological condition in which a...
By Michael Pokrovski 2026-04-18 06:36:33 0 2K
Economics
What causes economic growth?
Economic growth—the sustained increase in a country’s production of goods and...
By Leonard Pokrovski 2026-04-22 19:21:04 0 3K
Personal Finance
Are Student Loans Forgiven?
Are Student Loans Forgiven? Student loan forgiveness is one of the most discussed and...
By Leonard Pokrovski 2025-12-16 21:01:09 0 16K
Personal Finance
What Filing Status Should I Choose?
What Filing Status Should I Choose? “Is Married Filing Jointly better than...
By Leonard Pokrovski 2025-11-20 17:03:04 0 8K
Life Issues
Before Sunrise. (1995)
A young man and woman meet on a train in Europe, and wind up spending one evening together in...
By Leonard Pokrovski 2023-03-20 20:33:26 0 27K

BigMoney.VIP Powered by Hosting Pokrov