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
Best Feedback Collection Formats: Choosing What Works for You
Collecting feedback is essential for improving performance, products, services, and...
By Dacey Rankins 2025-07-28 14:53:30 0 8K
Decision Making and Problem Solving
What are the most important mental skills?
You blow a gasket over a minor error, watch your focus evaporate under pressure, or stare...
By Michael Pokrovski 2026-07-21 16:39:21 0 481
Социальные проблемы
Довод. Tenet. (2020)
После теракта в киевском оперном театре агент ЦРУ объединяется с британской разведкой, чтобы...
By Nikolai Pokryshkin 2022-10-15 22:43:39 0 39K
Economics
How Can Development Economics Help Solve Global Problems?
How Can Development Economics Help Solve Global Problems? Development economics is a field...
By Leonard Pokrovski 2026-03-17 02:51:45 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 9K

BigMoney.VIP Powered by Hosting Pokrov