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.

Search
Categories
Read More
FAQs, Help, and Tutorials
How to Make Money as a Programmer: Ways to Make Money Writing Code
How to Make Money as a Programmer: Ways to Make Money Writing Code Let's talk about the main...
By Leonard Pokrovski 2024-03-22 20:28:36 0 29K
Relationships
Gpykin: Where Real Connections Begin
A New Era of Online Dating In a world where people are busier, more mobile, and more digitally...
By Dating Gpykin 2025-11-05 21:19:18 0 337
Programming
Python Append List
To append a list, the variable must be a list type. Once the variable is a list, you can then...
By Jesse Thomas 2023-02-21 23:16:43 0 11K
Mental Health
Dementia: Pre-dementia
Pre-dementia states include pre-clinical and prodromal stages. The prodromal stages includes (1)...
By Kelsey Rodriguez 2023-07-17 17:52:05 0 14K
Телевидение
Прямой эфир Матч ТВ
Матч ТВ – российский федеральный общедоступный канал о спорте и здоровом образе жизни. Матч...
By Nikolai Pokryshkin 2022-08-27 17:46:39 0 35K

BigMoney.VIP Powered by Hosting Pokrov