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
Arts, Culture and Entertainment
Rifkin's Festival (2020)
The film tells the story of an American couple attending the San Sebastian International Film...
Par Leonard Pokrovski 2022-10-01 21:21:36 0 28KB
Научная фантастика и фэнтези
Чёрная вдова. Black Widow. (2021)
Наташе Романофф предстоит лицом к лицу встретиться со своим прошлым. Чёрной Вдове придется...
Par Nikolai Pokryshkin 2022-09-11 22:38:36 0 23KB
История
Куда ты идешь, Аида? Quo vadis, Aida? (2020)
Аида — переводчица при нидерландском контингенте войск ООН. В Сребренице живут её муж и...
Par Nikolai Pokryshkin 2022-09-11 21:38:16 0 33KB
Искусство, культура и развлечения
Поющие под дождём. Singin' in the Rain. (1952)
Основа этого легендарного фильма – дюжина старых песен, написанных для различных...
Par Nikolai Pokryshkin 2023-01-07 11:22:38 0 29KB
Business
What Type of Legal Structure Should Our Partnership Have?
Choosing the right legal structure is one of the most important decisions you’ll ever make...
Par Dacey Rankins 2025-12-01 15:06:52 0 3KB

BigMoney.VIP Powered by Hosting Pokrov