JavaScript set()

0
6K

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.

Căutare
Categorii
Citeste mai mult
Телевидение
Христианский телеканал GNC
GNC — Первый Христианский спутниковый телеканал на русском и украинском языках,...
By Nikolai Pokryshkin 2022-08-30 10:47:57 0 21K
Humanities
ETHNO-ART AS AN ACTUAL DIRECTION IN THE MODERN HUMANITIES
According to the concept common in the postmodern theory of art, the future is not in national...
By FWhoop Xelqua 2022-12-03 17:18:43 0 14K
Programming
How to Get Normally Distributed Random Numbers With NumPy
Probability distributions describe the likelihood of all possible outcomes of an event or...
By Jesse Thomas 2023-05-02 20:46:08 0 6K
Sport
Kill Bill: Vol. 1. (2003)
After awakening from a four-year coma, a former assassin wreaks vengeance on the team of...
By Leonard Pokrovski 2023-02-24 17:06:46 0 13K
Business
What is Sustainability?
Sustainability has become one of the most significant concepts of the modern era. With growing...
By Dacey Rankins 2025-02-03 15:58:12 0 3K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov