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.

Rechercher
Catégories
Lire la suite
Performing Arts
Preforming Arts
Performing arts is one of the types of artistic and creative activity in which the works of the...
Par FWhoop Xelqua 2023-07-10 17:26:36 0 18KB
Personal Finance
What Is the Difference Between Salary and Wage?
What Is the Difference Between Salary and Wage? Understanding the difference between salary and...
Par Leonard Pokrovski 2025-12-02 21:47:16 0 4KB
Life Issues
Finding Nemo. (2003)
After his son is captured in the Great Barrier Reef and taken to Sydney, a timid clownfish sets...
Par Leonard Pokrovski 2023-02-28 15:35:47 0 21KB
Business and Corporate Finance
What Are the Biggest Challenges in Corporate Finance Today?
What Are the Biggest Challenges in Corporate Finance Today? Volatility, Capital Access, and...
Par Leonard Pokrovski 2026-01-24 18:38:49 0 3KB
Social Issues
Full Metal Jacket. (1987)
A pragmatic U.S. Marine observes the dehumanizing effects the Vietnam War has on his fellow...
Par Leonard Pokrovski 2023-01-22 22:24:01 0 24KB

BigMoney.VIP Powered by Hosting Pokrov