JavaScript set()

0
8K

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.

Căutare
Categorii
Citeste mai mult
Business
Product placement: what is, types, goals
In this article, we will learn what product placement is, what it allows you to achieve, what...
By Dacey Rankins 2024-09-27 14:11:40 0 26K
Health
13 gift options for a person leading a healthy lifestyle
Leading a healthy lifestyle has become a fashionable trend among the population of the whole...
By FWhoop Xelqua 2022-10-17 13:05:47 0 37K
Mental Health
Autism Spectrum: Repetitive Behaviours
ASD includes a wide variety of characteristics. Some of these include behavioral characteristics...
By Kelsey Rodriguez 2023-02-16 15:28:10 0 8K
Television
Tulalip Live TV. USA.
TULALIP TV provides programming to Tulalip Tribal members and households around Indian country....
By Nikolai Pokryshkin 2022-10-28 10:37:33 0 23K
News and Media
News and Media: Navigating a Changing Landscape
The news and media industry has undergone a dramatic transformation in the digital age. From the...
By Dacey Rankins 2024-10-16 16:08:27 0 14K

BigMoney.VIP Powered by Hosting Pokrov