JavaScript set()

0
11KB

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.

Rechercher
Catégories
Lire la suite
Marketing and Advertising
How Do I Create a Positioning Statement? (A Step-by-Step Framework for Crafting Brand Clarity)
Introduction: Why Every Brand Needs a Clear Positioning Statement In the chaos of modern...
Par Dacey Rankins 2025-10-23 15:31:33 0 5KB
Financial Services
Changes in equilibrium price and quantity: the four-step process
Key points There is a four-step process that allows us to predict how an event...
Par Mark Lorenzo 2023-04-10 20:19:42 0 23KB
Business
What Does a Typical Day in This Role Look Like?
Helping Candidates Understand the Daily Responsibilities and Expectations When preparing for a...
Par Dacey Rankins 2025-06-19 18:47:50 0 11KB
Business
What Tools and Methods Support PMMs?
Product Marketing Managers (PMMs) juggle a wide variety of responsibilities: market research,...
Par Dacey Rankins 2025-09-15 18:56:54 0 8KB
Earth Sciences
Precipitation
In meteorology, precipitation is any product of the condensation of atmospheric water vapor that...
Par FWhoop Xelqua 2023-05-18 17:27:08 0 24KB

BigMoney.VIP Powered by Hosting Pokrov