JavaScript set()

0
6K

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.

Search
Categories
Read More
Социальные проблемы
Напоказ. Appearances. (2020)
Эва, героиня фильма «Напоказ», молодая счастливая женщина, мать очаровательного...
By Nikolai Pokryshkin 2022-10-10 18:15:51 0 20K
Business
Digital marketing
Digital marketing is a way to promote goods and services over the Internet. To find new...
By Dacey Rankins 2024-09-03 18:43:08 0 8K
Научная фантастика и фэнтези
Райя и последний дракон. Raya and the Last Dragon. (2021)
Давным-давно в волшебной стране Кумандре бок о бок с людьми жили драконы — создатели и...
By Nikolai Pokryshkin 2022-09-21 18:57:43 0 19K
Business
What is a Recent Trend Relating to Sustainability Worldwide?
Sustainability has become a central focus across industries globally, with various innovations...
By Dacey Rankins 2025-02-04 15:36:17 0 2K
Financial Services
What are public goods?
Key points A public good has two key characteristics: it is...
By Mark Lorenzo 2023-05-12 19:29:33 0 6K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov