JavaScript set()

0
10KB

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
Hospitality
The Future of Hospitality: Trends and Innovations Shaping the Industry in 2024
The hospitality industry is one of the most dynamic sectors of the global economy, constantly...
Por Dacey Rankins 2024-11-06 18:06:56 0 10KB
Business
How Does a Social Enterprise Differ from a Nonprofit?
In a world where organizations strive to address pressing social and environmental issues, two...
Por Dacey Rankins 2025-04-15 21:13:34 0 8KB
Телевидение
6ТВ прямая трансляция, г. Горловка.
Телеканал «6ТВ» г. Горловка - первооткрыватель ТВ-пространства в Горловке. За годы...
Por Nikolai Pokryshkin 2022-11-14 19:10:38 0 36KB
Mental Health
Autism Spectrum: Diagnosis Pt. 1
Autism spectrum disorder is a clinical diagnosis that is typically made by a physician based on...
Por Kelsey Rodriguez 2023-03-02 16:27:36 0 11KB
Business
What Traits or Qualities Define a Great Leader?
Great leaders combine vision, empathy, and strategic thinking to guide teams toward success....
Por Dacey Rankins 2025-08-13 20:13:17 0 2KB

BigMoney.VIP Powered by Hosting Pokrov