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.

Căutare
Categorii
Citeste mai mult
Жизненные вопросы
Перед рассветом. Before Sunrise. (1995)
Молодой американец Джесси знакомится в поезде с красивой француженкой Селин. Они сразу находят...
By Nikolai Pokryshkin 2023-03-19 14:14:00 0 30K
Track and Field
Track and Field: Embracing the Essence of Athleticism
Track and field, the epitome of athletic prowess and human achievement, encompasses a diverse...
By Dacey Rankins 2024-07-12 17:24:30 0 23K
Business
What is the difference between branding, performance and brand formation
Marketing is a multifaceted discipline that involves a variety of strategies and tactics. Among...
By Dacey Rankins 2024-09-02 19:22:52 0 13K
Personal Finance
What Is the 50/30/20 Budgeting Rule?
What Is the 50/30/20 Budgeting Rule? Managing money effectively can feel complicated, especially...
By Leonard Pokrovski 2025-10-20 16:56:17 0 783
Social Issues
North by Northwest. (1959)
A New York City advertising executive goes on the run after being mistaken for a government agent...
By Leonard Pokrovski 2023-01-17 19:04:53 0 22K

BigMoney.VIP Powered by Hosting Pokrov