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 Much Does SEM Cost? A Complete Guide to CPC, Budget Allocation, and Campaign Costs
One of the first questions businesses ask before launching paid search campaigns is: How much...
Par Dacey Rankins 2026-02-18 17:16:31 0 2KB
Business
Tips for Running Effective Meetings
Meetings are essential tools for collaboration, decision-making, and alignment. However, without...
Par Dacey Rankins 2025-06-02 16:35:40 0 10KB
Economics
What Is the Difference Between Comparative Economics and Economics?
What Is the Difference Between Comparative Economics and Economics? Economics is a broad field...
Par Leonard Pokrovski 2026-03-02 19:58:50 0 984
Entertainment
The Intersection of Shopping and Entertainment: A Retail Revolution
The Intersection of Shopping and Entertainment: A Retail Revolution Introduction:In the evolving...
Par Leonard Pokrovski 2024-05-30 00:11:51 0 26KB
Marketing and Advertising
What Does PPC Stand For?
PPC is one of the most commonly used terms in digital marketing, yet many people encounter it...
Par Dacey Rankins 2026-02-03 19:07:58 0 2KB

BigMoney.VIP Powered by Hosting Pokrov