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
Business
What Are Common Mistakes When Using User Stories?
User stories are one of the most widely adopted tools in Agile product development. They provide...
Par Dacey Rankins 2025-08-26 11:42:49 0 8KB
Stories
The History of Movies: A Journey Through Time and Technology
The history of movies is a fascinating story of creativity, technological innovation, and...
Par Dacey Rankins 2025-01-24 18:14:52 0 16KB
Business
Tourists went into outer space for the first time in history. How this industry is developing
On September 12, the first ever tourist spacewalk took place. As part of the private mission...
Par Dacey Rankins 2024-09-24 14:47:56 0 21KB
Finance
What is the Difference Between a Checking Account and a Savings Account?
What is the Difference Between a Checking Account and a Savings Account? When it comes to...
Par Leonard Pokrovski 2025-09-30 21:49:18 0 4KB
Life Issues
Rear Window. (1954)
A wheelchair-bound photographer spies on his neighbors from his Greenwich Village courtyard...
Par Leonard Pokrovski 2022-12-09 19:36:34 0 30KB

BigMoney.VIP Powered by Hosting Pokrov