JavaScript set()

0
11KB

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.

Rechercher
Catégories
Lire la suite
Earth Sciences
Unveiling Earth's Secrets: Exploring the Wonders of Earth Sciences
Unveiling Earth's Secrets: Exploring the Wonders of Earth Sciences Earth, our home planet, is a...
Par Leonard Pokrovski 2024-05-22 20:43:46 0 20KB
Personal Finance
How Do I Create a Budget?
How Do I Create a Budget? Creating a budget is one of the most powerful steps you can take to...
Par Leonard Pokrovski 2025-10-20 16:55:33 0 2KB
Biology
Scarlet Macaw
Scarlet Macaw (Ara Macao) – With a spectacular plumage and an inquisitive nature these...
Par FWhoop Xelqua 2023-05-15 20:37:55 0 23KB
Деньги
Услуги, направленные на привлечение финансового изобилия или улучшение финансового благополучия от Архипокровителя Николая.
1. Денежные заклинания.- Заклинания изобилия: Ритуалы, направленные на проявление богатства и...
Par Nikolai Pokrovski 2024-12-19 20:50:32 0 21KB
Programming
Python TypeError
The Python TypeError is an exception that occurs when the data type of an object in an...
Par Jesse Thomas 2023-03-24 21:03:52 0 11KB

BigMoney.VIP Powered by Hosting Pokrov