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.

Buscar
Categorías
Read More
Money
Definition of AER (Annual Equivalent Rate)
Definition of AER (Annual Equivalent Rate) The Annual Equivalent Rate (AER) is a standard way of...
By Leonard Pokrovski 2025-09-26 20:38:14 0 12K
Business
How Can I Stay Motivated or Improve Emotional Intelligence?
Motivation and emotional intelligence (EQ) are two pillars of success, both in personal growth...
By Dacey Rankins 2025-09-08 08:03:12 0 5K
Business
How Can I Find a Suitable Mentor?
Finding the right mentor can be a transformative step in your personal and professional journey....
By Dacey Rankins 2025-05-24 15:40:45 0 8K
Marketing and Advertising
How Do Ads Make Money?
Advertising is one of the largest industries in the world, generating hundreds of billions of...
By Dacey Rankins 2026-01-05 23:54:49 0 2K
Business
What Is Mentoring?
Mentoring is a powerful and transformative process centered on a longer-term relationship between...
By Dacey Rankins 2025-07-07 14:41:16 0 4K

BigMoney.VIP Powered by Hosting Pokrov