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
Financial Services
The Keynesian perspective on market forces
Key points The Keynesian prescription for stabilizing the economy implies...
Par Mark Lorenzo 2023-06-22 20:30:19 0 12KB
Business
How Does the Company Celebrate Successes and Recognize Employee Contributions?
A thriving organization understands that success is a collective effort and that recognizing the...
Par Dacey Rankins 2025-06-12 14:49:53 0 14KB
Business
Fake Ad Blocker Capable of Running Arbitrary code on Windows Systems
ESET specialists have discovered an advertising malware that is distributed under the guise of an...
Par Dacey Rankins 2024-07-25 10:04:51 0 16KB
Business
Is Google Analytics Free to Use?
One of the reasons Google Analytics has become the most popular web analytics tool in the world...
Par Dacey Rankins 2025-08-30 00:38:48 0 8KB
Marketing and Advertising
What Is Google Remarketing? A Complete Guide for Modern Advertisers
If you have ever visited a website and later seen ads for that same brand while browsing other...
Par Dacey Rankins 2026-02-11 23:55:36 0 4KB

BigMoney.VIP Powered by Hosting Pokrov