JavaScript set()

0
12χλμ.

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.

Αναζήτηση
Κατηγορίες
Διαβάζω περισσότερα
Business
How developers create advertising
Everything is stableSince real estate is not a cheap commodity, its advertising, as a rule, is...
από Dacey Rankins 2024-09-13 19:32:27 0 15χλμ.
Социальные проблемы
Город Бога. City of God. (2002)
Парень по кличке Ракета, балансирующий между честной жизнью и мелкими правонарушениями, и его...
από Nikolai Pokryshkin 2022-11-18 16:41:21 0 44χλμ.
Συνδεδεμένος
Top 5 popular games of 2023
2023 is looking to be a great year for games. Therefore, we have created a list of the most...
από FWhoop Xelqua 2023-05-05 15:24:26 0 22χλμ.
Economics
Why are prices and investments changing?
Why Are Prices and Investments Changing? The Number on the Tag Is Never the Whole Story Walk...
από Leonard Pokrovski 2026-06-12 00:39:50 0 553
Nature and Science
Grizzly Man (2005)
A devastating and heart-rending take on grizzly bear activists Timothy Treadwell and Amie...
από Leonard Pokrovski 2023-07-28 20:39:14 0 525χλμ.

BigMoney.VIP Powered by Hosting Pokrov