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
Marketing and Advertising
What Industries Benefit Most from TV Advertising?
Television advertising has long been associated with large national brands, but the reality is...
By Dacey Rankins 2026-02-23 13:17:20 0 2K
Business
What Are the Most Critical Decisions You Make as a Leader?
Leadership is not just about holding a title or managing people—it’s about making...
By Dacey Rankins 2025-05-09 15:02:26 0 9K
Business
Personal Brand: Impact on Business Success
A personal brand helps to stand out from competitors and plays a key role in promoting goods and...
By Dacey Rankins 2024-09-11 15:27:22 0 17K
Marketing and Advertising
Is Billboard Advertising Effective?
In an era dominated by smartphones, social media, and data-driven digital advertising, many...
By Dacey Rankins 2026-01-13 15:23:28 0 5K
Marketing and Advertising
How Can Storytelling Be Used in Business, Marketing, and Brand Communication?
Storytelling isn’t just for books or movies — it’s one of the most powerful...
By Dacey Rankins 2025-11-04 17:39:32 0 10K

BigMoney.VIP Powered by Hosting Pokrov