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
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 13K
Financial Services
Perfect Competition And Why It Matters
Key points A perfectly competitive firm is a price taker, which means that it...
By Mark Lorenzo 2023-02-06 13:41:15 0 13K
South America
Attractions in South America
Attractions in South America:Salar de Uyuni (Bolivia)Church of Las Lajas (Colombia)Lake Titicaca...
By FWhoop Xelqua 2023-01-23 17:09:42 0 21K
Ужасы
Воспоминания об убийстве. Memories of Murder. (2003)
В 1986 году в провинциальном городе Хвасон две местные женщины были убиты одним и тем же...
By Nikolai Pokryshkin 2023-04-02 20:10:40 0 44K
Marketing and Advertising
How Do We Differentiate Ourselves from Competitors (Unique Selling Proposition / Positioning)?
Introduction In today’s crowded marketplace, standing out is no longer optional —...
By Dacey Rankins 2025-10-15 19:06:14 0 2K

BigMoney.VIP Powered by Hosting Pokrov