JavaScript set()

0
11K

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
Социальные проблемы
Рождество кота Боба. A Christmas Gift from Bob. (2020)
До встречи с котом Бобом Джеймс не любил Рождество, но рыжий пушистый хулиган всё изменил. Он в...
By Nikolai Pokryshkin 2022-10-05 17:02:02 0 24K
Business
Interview Questions and Answers for a Chief Executive Officer
Hiring a Chief Executive Officer (CEO) is one of the most critical decisions a board of directors...
By Dacey Rankins 2025-06-16 15:02:59 0 5K
Business
10 Examples of Gamification in Building Customer Relationships
What is relationship selling?The term "relationship sales" describes an approach that is the...
By Dacey Rankins 2024-09-09 19:37:26 0 13K
Chemistry
TOP 10 discoveries and events in the chemical industry in 2021
Another difficult year is behind us. Political, socio-economic and climatic changes every day...
By FWhoop Xelqua 2023-01-25 16:19:56 0 22K
Tchoukball
Discovering Tchoukball: A Unique Fusion of Sportsmanship and Strategy
In the realm of lesser-known sports that blend athleticism with tactical prowess, Tchoukball...
By Dacey Rankins 2024-07-11 18:09:18 0 20K

BigMoney.VIP Powered by Hosting Pokrov