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.

Cerca
Categorie
Leggi tutto
Business
Frequently Asked Questions on Conflict Resolution and Mediation
Conflict is a natural part of human interaction, whether in the workplace, community, or personal...
By Dacey Rankins 2025-06-10 15:21:17 0 13K
Marketing and Advertising
What Is a Skippable YouTube Ad?
A skippable YouTube ad is a video advertisement that plays before, during, or after a video on...
By Dacey Rankins 2026-03-02 19:49:43 0 5K
Marketing and Advertising
Which Platforms Should I Use for Video Marketing? Choosing the Right Channels for Maximum Impact
Introduction Video marketing is one of the most effective ways to engage audiences, build trust,...
By Dacey Rankins 2025-11-07 20:09:51 0 9K
Business
Should We Partner in a For-Profit or Nonprofit Capacity?
Forming a partnership—especially between a for-profit business and a nonprofit...
By Dacey Rankins 2025-12-02 18:48:26 0 8K
Marketing and Advertising
How Do Startups Acquire Customers?
Customer acquisition is arguably the most critical challenge for startups. No matter how...
By Dacey Rankins 2026-01-16 16:39:43 0 8K

BigMoney.VIP Powered by Hosting Pokrov




google-site-verification=q_0zhvNWwTfwbQ-uEJsvcmUKKbtOWUTgl3H57qDiFrg