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
Social Issues
The Vault (2021)
Follows a genius engineering graduate who is interested in the Bank of Spain's safe. My Link
Par Leonard Pokrovski 2022-09-14 20:38:07 0 29KB
Business
How Is the Success of an Influencer Marketing Campaign Measured?
Influencer marketing has become one of the most powerful tools for brands to connect with their...
Par Dacey Rankins 2025-02-27 17:07:04 0 14KB
Природа и наука
Принцесса Мононоке. Princess Mononoke. (1997)
Убив вепря, юный принц Аситака навлек на себя смертельное проклятие. Старая знахарка предсказала,...
Par Nikolai Pokryshkin 2023-01-03 22:51:08 0 29KB
Products and Shopping
A Comprehensive Guide to Health Products: Enhancing Wellness Naturally
In today’s fast-paced world, where people are constantly juggling work, family, and social...
Par Dacey Rankins 2024-11-22 14:50:52 0 13KB
Financial Services
Beyond GDP: other ways to measure the economy
Key points Gross national product, or GNP, includes what is produced...
Par Mark Lorenzo 2023-06-06 19:49:43 0 22KB

BigMoney.VIP Powered by Hosting Pokrov