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
Mental Health
Dyslexia: Associated conditions
Dyslexia often co-occurs with other learning disorders, but the reasons for this comorbidity have...
Par Kelsey Rodriguez 2023-06-16 19:07:03 0 12KB
Financial Services
Supply and its determinants
The law of supply The law of supply states that there is a positive...
Par Mark Lorenzo 2023-02-24 17:17:03 0 13KB
Productivity
How do I prioritize tasks effectively?
Prioritizing tasks effectively is one of the most critical skills in time management and...
Par Michael Pokrovski 2026-02-28 02:34:56 0 1KB
Television
WKRG News 5 Live Newscast stream TV. USA.
WKRG NEWS 5 LIVE NEWSCAST SCHEDULE:MORNING NEWS: Weekday mornings 4:30 – 7 a.m.MIDDAY...
Par Nikolai Pokryshkin 2022-11-24 15:26:42 0 32KB
Business
Why Is Management Science Important for Decision-Making?
Management Science plays a critical role in modern organizations because it brings structure,...
Par Dacey Rankins 2025-08-15 20:27:21 0 4KB

BigMoney.VIP Powered by Hosting Pokrov