JavaScript set()

0
9K

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.

Cerca
Categorie
Leggi tutto
Sport
Raging Bull. (1980)
The life of boxer Jake LaMotta, whose violence and temper that led him to the top in the ring...
By Leonard Pokrovski 2023-03-02 18:42:28 0 19K
Financial Services
Beyond salary: benefits may matter more than you think
So you’ve just gotten your first job offer. While salary may be your main focus,...
By Mark Lorenzo 2023-05-29 19:36:13 0 19K
История
Иди и смотри. (1985)
1943 год, Великая Отечественная война. Подросток Флёра, откопав карабин среди обрывков колючей...
By Nikolai Pokryshkin 2023-01-12 19:19:09 0 25K
Социальные проблемы
Мясо и кровь. Wildland. (2020)
Ида, после гибели своей матери в автокатастрофе, переезжает жить к своей тёте и двоюродным...
By Nikolai Pokryshkin 2022-10-19 17:58:26 0 24K
Transportation and Logistics
Understanding Transportation and Logistics: The Backbone of Global Trade
Transportation and logistics are two closely intertwined sectors that are essential for the...
By Dacey Rankins 2024-11-25 14:21:22 0 9K

BigMoney.VIP Powered by Hosting Pokrov



Top sites Pokrov