JavaScript set()

0
10Кб

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.

Поиск
Категории
Больше
Financial Services
Elasticity in areas other than price
Key points Elasticity is a general term, referring to percentage change of one...
От Mark Lorenzo 2023-04-14 17:08:27 0 12Кб
Aviation
The fastest aircraft in the world and its competitors - How fast do they fly?
Modern aircraft are able to fly very fast. By "fast" I don't mean speeds 10 times faster than a...
От FWhoop Xelqua 2022-10-25 11:02:46 0 22Кб
Internet
7 Ways to Become a Clubhouse Star
Fill out your profile correctly. Before accepting a lesson in rooms or in creating them, it is...
От FWhoop Xelqua 2023-06-03 18:08:24 0 24Кб
Искусство, культура и развлечения
Крещендо. Crescendo. (2020)
Знаменитый немецкий дирижер Эдвард Спор принимает приглашение устроительницы гуманитарного...
От Nikolai Pokryshkin 2022-09-29 23:03:03 0 31Кб
Alternative
Results of the second CAIPAN seminar from CNES
The second seminar of the French Space Agency (CNES) CAIPAN 2, held in Toulouse on October 13-14,...
От FWhoop Xelqua 2022-12-12 15:22:20 0 18Кб

BigMoney.VIP Powered by Hosting Pokrov