JavaScript set()

0
11K

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
Social Issues
Monster (2021)
Monster tells the story of Steve Harmon (Kelvin Harrison, Jr.) a seventeen-year-old honor student...
By Leonard Pokrovski 2022-09-07 13:37:58 0 36K
Marketing and Advertising
Can Small Businesses Use Billboard Advertising?
Billboard advertising has traditionally been associated with large brands, national campaigns,...
By Dacey Rankins 2026-01-15 17:22:26 0 749
Business
How Often Should I Ask for Feedback?
Asking for feedback is one of the most powerful habits a professional or leader can...
By Dacey Rankins 2025-07-28 14:51:58 0 3K
Business
How Do I Migrate from Universal Analytics to GA4?
With the official sunset of Universal Analytics (UA) in July 2023, businesses that relied on UA...
By Dacey Rankins 2025-09-01 09:25:37 0 4K
Marketing and Advertising
Is Going Viral a Guarantee of Success or Sales?
Introduction In today’s hyperconnected world, “going viral” is the dream of...
By Dacey Rankins 2025-11-11 18:01:46 0 3K

BigMoney.VIP Powered by Hosting Pokrov