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
Argent
What is a balloon payment?
What is a balloon payment? A balloon payment is a large, lump-sum payment due at the end of...
Par Leonard Pokrovski 2025-09-27 16:48:35 0 6KB
Archery
Archery
 Archery is an Olympic sport in which athletes compete in archery accuracy. The winner in...
Par FWhoop Xelqua 2022-09-23 17:03:37 0 35KB
Социальные проблемы
Сама по себе. Herself. (2020)
Молодая мать сбегает с дочерьми от жестокого мужа. Вопреки государственной жилищной системе она...
Par Nikolai Pokryshkin 2022-10-05 17:32:59 0 60KB
Спорт
Убить Билла 1-2. Kill Bill: Vol. 1 - 2. (2003-2004)
Убить Билла 1. Kill Bill: Vol. 1. (2003)В беременную наемную убийцу по кличке Черная Мамба во...
Par Nikolai Pokryshkin 2023-02-23 18:52:53 0 25KB
Life Issues
Baby Money. (2021)
When a home invasion turns into a bloody shoot-out, a pair of ragtag fugitives take refuge in the...
Par Leonard Pokrovski 2023-05-10 18:55:01 0 43KB

BigMoney.VIP Powered by Hosting Pokrov