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
Health
What is Health?
What is Health? And really, what does it mean to be healthy? Most will answer: not...
By Leonard Pokrovski 2024-04-10 00:59:19 0 13K
Mental Health
Dyslexia: Associated conditions
Dyslexia often co-occurs with other learning disorders, but the reasons for this comorbidity have...
By Kelsey Rodriguez 2023-06-16 19:07:03 0 11K
Personal Finance
We conduct an audit of assets and liabilities
What will you learn?   What is personal capital   How to Build a...
By FWhoop Xelqua 2022-10-07 15:36:52 0 26K
Personal Finance
How Do I Claim Dependents?
How Do I Claim Dependents? Who Can I Claim as a Dependent & What Are the Rules? Claiming...
By Leonard Pokrovski 2025-11-21 19:39:03 0 1K
Business
How Do You Define and Measure KPIs?
Key Performance Indicators (KPIs) are among the most widely used tools in business performance...
By Dacey Rankins 2025-08-27 14:59:14 0 4K

BigMoney.VIP Powered by Hosting Pokrov