JavaScript set()

0
11KB

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.

Pesquisar
Categorias
Leia mais
Mental Health
Dementia: Vascular
Vascular dementia accounts for at least 20% of dementia cases, making it the second most common...
Por Kelsey Rodriguez 2023-07-26 19:30:19 0 16KB
Arts, Culture and Entertainment
Searching for Sugar Man (2012)
Two South Africans set out to discover what happened to their unlikely musical hero, the...
Por Leonard Pokrovski 2023-07-10 20:24:48 0 23KB
Financial Services
How to establish an emergency savings fund
Establishing an emergency savings fund now can have a big payoff later. Setting aside emergency...
Por Mark Lorenzo 2023-05-17 20:13:55 0 17KB
Programming
Python pow()
The pow() method is a math built-in method where it calculates the 2 numbers. The pow() method...
Por Jesse Thomas 2023-04-13 18:09:50 0 11KB
Business
What Skills Does a Growth Hacker Need?
In today’s competitive digital landscape, businesses are constantly seeking ways to grow...
Por Dacey Rankins 2025-09-11 16:29:54 0 3KB

BigMoney.VIP Powered by Hosting Pokrov