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.

Site içinde arama yapın
Kategoriler
Read More
Programming
Functions and Classes in a function
Today, I've learned that you can apparently put functions and class methods inside of a function...
By Jesse Thomas 2023-01-31 19:52:59 0 11K
Искусство, культура и развлечения
Поющие под дождём. Singin' in the Rain. (1952)
Основа этого легендарного фильма – дюжина старых песен, написанных для различных...
By Nikolai Pokryshkin 2023-01-07 11:22:38 0 28K
Gaelic
Gaelic: Unveiling the Rich Heritage and Enduring Legacy of Ireland's Indigenous Culture
  Gaelic: Unveiling the Rich Heritage and Enduring Legacy of...
By Leonard Pokrovski 2024-06-25 16:50:48 0 24K
Business
How Long Does It Take to See Retention Improvements?
When companies start investing in user retention strategies, one of the first questions...
By Dacey Rankins 2025-09-16 16:32:29 0 5K
Искусство, культура и развлечения
Тайна Коко. Coco. (2017)
12-летний Мигель живёт в мексиканской деревушке в семье сапожников и тайно мечтает стать...
By Nikolai Pokryshkin 2022-12-29 13:00:25 1 27K

BigMoney.VIP Powered by Hosting Pokrov