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
Социальные проблемы
Атлетка А: Скандал в американской гимнастике. Athlete A. (2020)
История одного из самых крупных скандалов в профессиональном спорте в США. Врач сборной США по...
By Nikolai Pokryshkin 2022-10-18 14:10:06 0 32K
Personal Finance
What Happens If I Delay Social Security?
What Happens If I Delay Social Security? Understanding Delayed Retirement Credits and How Much...
By Leonard Pokrovski 2025-12-12 22:27:00 0 2K
Analysis and Opinion
How to Use Critical Thinking in the Information Flow
What is Critical ThinkingCritical thinking is the ability to doubt incoming information and one's...
By FWhoop Xelqua 2022-09-25 10:47:13 0 29K
Business
How Do I Overcome Procrastination?
Procrastination is the silent thief of time. Most of us have experienced it: putting off a task...
By Dacey Rankins 2025-09-08 08:04:00 0 4K
Business
Who Should Be Invited to the Meeting?
Why Selecting the Right Participants Drives Better Results Meetings are an essential part of...
By Dacey Rankins 2025-05-30 14:54:21 0 7K

BigMoney.VIP Powered by Hosting Pokrov