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
Does Automation Replace Jobs?
This is one of the most common and important questions about automation—and the honest...
By Dacey Rankins 2026-04-27 21:09:14 0 736
Financial Services
Price Ceilings and Price Floors
Key points Price ceilings prevent a price from rising above a certain level....
By Mark Lorenzo 2023-01-30 14:40:25 0 15K
Marketing and Advertising
How Does Outdoor Advertising Support Brand Awareness?
Brand awareness is about familiarity, recognition, and trust—and outdoor advertising is...
By Dacey Rankins 2026-02-03 18:43:16 0 4K
Music
8 Calming Acoustic Music Sessions with Soft Drums for a Serene Atmosphere of Focus and Relaxation
In today’s fast-paced world, finding a moment of peace can feel like a luxury. Whether...
By Gpykin 2025-07-27 10:35:25 0 27K
Productivity
What policies support work-life balance?
What Policies Support Work-Life Balance? Work-life balance has evolved from a desirable perk...
By Michael Pokrovski 2026-03-31 18:50:44 0 2K

BigMoney.VIP Powered by Hosting Pokrov