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
Business
How Can Late-Stage Startups Maintain Innovation?
As startups transition into late-stage companies, maintaining innovation becomes a challenge....
By Dacey Rankins 2025-03-07 13:42:46 0 15K
Economics
How Does Commerce Contribute to the Economy?
How Does Commerce Contribute to the Economy? Commerce is the system through which goods and...
By Leonard Pokrovski 2026-02-19 02:23:23 0 946
Marketing and Advertising
What Is Outdoor Advertising?
Outdoor advertising is one of the oldest and most recognizable forms of marketing, yet it remains...
By Dacey Rankins 2026-01-30 19:05:26 0 1K
Software
Working Out apps
Most running apps connect to your running watch or fitness tracker for accurate workout data....
By FWhoop Xelqua 2023-04-09 18:33:18 0 23K
Financial Services
Public goods: real-world examples
Key points Goods that are nonexcludable and rivalrous are called common...
By Mark Lorenzo 2023-05-16 19:34:56 0 11K

BigMoney.VIP Powered by Hosting Pokrov