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
Personal Finance
How Salary Ranges Work: Minimum, Midpoint, Maximum, and Pay Bands
How Salary Ranges Work: Minimum, Midpoint, Maximum, and Pay Bands Salary ranges are one of the...
By Leonard Pokrovski 2025-11-28 20:56:15 0 3K
Kids and Teens
Navigating the Challenges: Understanding Teen Struggles in 2024
In the ever-evolving landscape of adolescence, the challenges facing teenagers in 2024 are as...
By Dacey Rankins 2024-04-29 13:01:04 0 18K
Marketing and Advertising
Best Practices for Effective Email Marketing in 2023 (and Beyond)
Introduction Email marketing is one of the most reliable and profitable digital marketing...
By Dacey Rankins 2025-10-03 20:40:07 0 6K
Money
What is APR on a Credit Card?
What is APR on a Credit Card? When you’re comparing credit cards, one of the most...
By Leonard Pokrovski 2025-09-20 15:30:19 0 2K
Programming
Encoding and Decoding Base64 Strings in Python
The Base64 encoding is used to convert bytes that have binary or text data into ASCII characters....
By Jesse Thomas 2023-03-10 20:59:12 0 14K

BigMoney.VIP Powered by Hosting Pokrov