JavaScript set()

0
12KB

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.

Suche
Kategorien
Mehr lesen
Productivity
How do I avoid procrastination when managing tasks?
How Do I Avoid Procrastination When Managing Tasks? Procrastination is one of the biggest...
Von Michael Pokrovski 2026-03-19 13:41:19 0 5KB
Marketing and Advertising
What Are Common Billboard Advertising Mistakes?
Billboard advertising offers massive visibility, but visibility alone does not guarantee...
Von Dacey Rankins 2026-01-15 17:36:40 0 4KB
Marketing and Advertising
What Is Email Marketing? Strategies, Benefits, and Best Practices
Introduction Email marketing remains one of the most cost-effective and powerful tools in a...
Von Dacey Rankins 2025-10-02 20:06:21 0 7KB
Marketing and Advertising
What Should My Podcast Be About? How to Pick a Topic or Niche
Introduction: The Foundation of Every Great Podcast Before hitting record or buying your first...
Von Dacey Rankins 2025-10-22 15:48:37 0 5KB
Climbing
Mountain Climbing: The Pursuit of Adventure and Challenge
Mountain climbing is one of the most exhilarating and challenging outdoor activities, blending...
Von Dacey Rankins 2024-12-06 15:30:18 0 13KB

BigMoney.VIP Powered by Hosting Pokrov