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.

Suche
Kategorien
Mehr lesen
Научная фантастика и фэнтези
Звёздные войны. Эпизод VI: Возвращение Джедая. Star Wars: Episode VI - Return of the Jedi. (1983)
В шестом эпизоде «Звездных войн» Дарт Вейдер создает вторую «Звезду...
Von Nikolai Pokryshkin 2023-01-08 22:53:53 0 41KB
Programming
Python List Slicing
In Python, list slicing is a common practice and it is the most used technique for programmers to...
Von Jesse Thomas 2023-03-08 16:49:21 0 12KB
Business
What are the stages of crisis management?
Crisis management is the process organizations use to prepare for, respond to, and recover from...
Von Dacey Rankins 2025-01-17 16:49:33 0 12KB
Marketing and Advertising
How Do We Audit Past Marketing Efforts to Inform a Better Strategy?
Introduction A marketing strategy without reflection is like sailing without a compass. Even the...
Von Dacey Rankins 2025-10-16 19:03:01 0 4KB
Literature
The Power of Literature: Exploring the Depths of Human Experience
Literature is one of humanity's most cherished and enduring forms of expression. It encompasses...
Von Dacey Rankins 2024-12-13 14:28:38 0 11KB

BigMoney.VIP Powered by Hosting Pokrov