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
How Do Local Mobile Searches Behave / How to Optimize for “Near Me” Searches
Introduction “Near me” searches have become one of the defining behaviors of the...
By Dacey Rankins 2025-10-17 15:24:32 0 8K
Marketing and Advertising
How Do I Monetize a Podcast? (Ads, Sponsorships, and Listener Support)
Introduction: Turning Passion into Profit Podcasting often begins as a creative or professional...
By Dacey Rankins 2025-10-22 15:54:52 0 7K
Business
What Ethical and Bias Considerations Arise in Its Application?
While Management Science offers powerful tools for decision-making, ethical and bias issues can...
By Dacey Rankins 2025-08-15 20:28:13 0 8K
Marketing and Advertising
How Do I Reduce User Acquisition Cost?
Reducing user acquisition cost (CAC) is one of the most important levers for sustainable growth....
By Dacey Rankins 2026-02-27 18:25:47 0 6K
Business
What Moment Shaped Me Most?
Life is often described as a journey, and along that journey there are moments—sometimes...
By Dacey Rankins 2025-09-09 14:17:09 0 5K

BigMoney.VIP Powered by Hosting Pokrov