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
Schizophrenia Diagnosis and Comorbidities
CriteriaSchizophrenia is diagnosed based on criteria in either the Diagnostic and Statistical...
Par Kelsey Rodriguez 2023-02-02 16:14:08 0 12KB
Satire
Satirical News
Satirical (humorous, humorous, parody) news is completely or partially invented information about...
Par FWhoop Xelqua 2023-02-28 19:30:00 0 19KB
Business
YouTube Podcasts – A Trend That Determines Business Success
Podcasts as a global trend are currently on the rise and for good reason. We are talking about...
Par Dacey Rankins 2024-09-05 18:59:16 0 21KB
Телевидение
Телеканал "Продвижение" прямой эфир.
Семейно - развлекательный телеканал «Продвижение» создан в 2014 году. Аудитория...
Par Nikolai Pokryshkin 2022-11-26 20:54:38 0 28KB
Personal Finance
How Much Does Education Cost?
How Much Does Education Cost? Education is often described as an investment in the...
Par Leonard Pokrovski 2025-10-30 22:32:04 0 9KB

BigMoney.VIP Powered by Hosting Pokrov