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
How developers create advertising
Everything is stableSince real estate is not a cheap commodity, its advertising, as a rule, is...
Par Dacey Rankins 2024-09-13 19:32:27 0 15KB
Социальные проблемы
Ещё по одной. Another Round. (2020)
В ресторане собираются учитель истории, психологии, музыки и физрук, чтобы отметить 40-летие...
Par Nikolai Pokryshkin 2022-10-10 17:38:27 0 34KB
Business
What Are the Latest Trends in Entrepreneurship?
The world of entrepreneurship is constantly evolving, driven by technological advancements,...
Par Dacey Rankins 2025-03-18 13:53:48 0 10KB
Personal Finance
What Is FAFSA and How Do I Complete It?
What Is FAFSA and How Do I Complete It? Paying for college or career school can feel...
Par Leonard Pokrovski 2025-12-15 18:40:46 0 2KB
Научная фантастика и фэнтези
Война будущего. The Tomorrow War (2021)
Декабрь 2022 года. Во время финала чемпионата мира по футболу в Катаре на поле высаживается...
Par Nikolai Pokryshkin 2022-09-11 22:50:40 0 37KB

BigMoney.VIP Powered by Hosting Pokrov