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
Can digital detox improve focus?
Can Digital Detox Improve Focus? A “digital detox” refers to a deliberate period of...
By Michael Pokrovski 2026-04-18 06:53:05 0 17K
Marketing and Advertising
What Factors Affect Search Engine Rankings?
A Complete Guide to the Key Elements That Influence SEO Performance in 2025 When it comes to...
By Dacey Rankins 2025-10-31 15:42:09 0 18K
Life Issues
Three Billboards Outside Ebbing, Missouri. (2017)
A mother personally challenges the local authorities to solve her daughter's murder when they...
By Leonard Pokrovski 2023-03-08 17:39:08 0 23K
Productivity
How do I manage tasks when working from home?
How Do I Manage Tasks When Working From Home? Working from home has become increasingly common,...
By Michael Pokrovski 2026-03-19 20:48:22 0 11K
Business
What Frameworks Are Useful for Developing Product Strategy?
Developing a strong product strategy is one of the most critical responsibilities for product...
By Dacey Rankins 2025-08-21 16:47:26 0 7K

BigMoney.VIP Powered by Hosting Pokrov