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
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 15KB
Жизненные вопросы
Она говорит со мной. We Still Talk. (2021)
Вот уже 65 лет Нино влюблен в Рину и продолжает любить, хотя ее уже нет рядом. Он все еще...
Par Nikolai Pokryshkin 2022-09-16 21:59:50 0 26KB
Business
What Is the Product Planning Process?
Product planning is the backbone of effective product management. It transforms a vision into...
Par Dacey Rankins 2025-08-20 15:40:13 0 5KB
Home Automation
Finding Investors for the Smart Base for TV: A Revolutionary Home Entertainment Solution
In the ever-evolving landscape of home entertainment, innovation is key. The Smart Base for TV...
Par Vladimir Tsigrik 2024-11-01 17:10:07 0 17KB
Personal Finance
How Do I Qualify for a Mortgage?
How Do I Qualify for a Mortgage? Understanding Credit Score Requirements, Income, Employment...
Par Leonard Pokrovski 2025-11-14 21:38:16 0 6KB

BigMoney.VIP Powered by Hosting Pokrov