JavaScript set()

0
11K

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.

Site içinde arama yapın
Kategoriler
Read More
Mental Health
Autism Spectrum: Pathophysiology
  Autism's symptoms result from maturation-related changes in various systems of the brain....
By Kelsey Rodriguez 2023-02-24 17:39:41 0 11K
Arts, Culture and Entertainment
The Prom (2020)
In desperate need of a noble cause to revive their public images and bounce back, self-centred...
By Leonard Pokrovski 2022-09-26 20:48:09 0 28K
Business
What Is Logistics in Management Science?
Management of Flow of Goods, Services, or People Using Optimization, Analytics, Scheduling, etc....
By Dacey Rankins 2025-07-03 18:01:55 0 6K
Business
Why Do Growth Rates Mislead?
Growth rates are one of the most common metrics used to describe progress, whether in business,...
By Dacey Rankins 2025-09-09 14:38:42 0 4K
Business
How Can the Product Be More Environmentally Friendly or Accessible?
In today’s marketplace, environmental sustainability and accessibility are no longer...
By Dacey Rankins 2025-08-19 17:48:29 0 5K

BigMoney.VIP Powered by Hosting Pokrov