JavaScript set()

0
7K

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.

Buscar
Categorías
Read More
Rodeo
The Thrilling World of Rodeo: Where Grit and Glory Meet
In the heartland of America, where vast expanses of prairie stretch as far as the eye can see,...
By Dacey Rankins 2024-07-05 16:58:55 0 14K
Directorio
Zeitgeist (2007)
Mythology and belief in society today, presenting uncommon perspectives of common cultural...
By Leonard Pokrovski 2023-07-15 20:09:29 0 24K
Arts, Culture and Entertainment
The Prestige. (2006)
After a tragic accident, two stage magicians in 1890s London engage in a battle to create the...
By Leonard Pokrovski 2022-12-03 19:07:47 0 19K
Horror
Se7en. (1995)
Two detectives, a rookie and a veteran, hunt a serial killer who uses the seven deadly sins as...
By Leonard Pokrovski 2022-11-16 21:08:29 0 17K
Recreation
The Art of Shopping as Recreation: Exploring the Joy Beyond Purchases
Shopping has transcended its utilitarian roots to become a leisure activity in its own right. No...
By Dacey Rankins 2024-06-03 18:24:16 0 13K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov