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
Социальные проблемы
Довод. Tenet. (2020)
После теракта в киевском оперном театре агент ЦРУ объединяется с британской разведкой, чтобы...
By Nikolai Pokryshkin 2022-10-15 22:43:39 0 33K
Biography
How to write an autobiography
An autobiography is a short story about yourself, which allows you to find out general...
By FWhoop Xelqua 2022-10-22 12:07:43 0 24K
Business
How Would You Describe the Company Culture?
Company culture is more than a buzzword—it’s the foundation that shapes how employees...
By Dacey Rankins 2025-08-09 18:27:40 0 5K
Life Issues
Before Sunrise. (1995)
A young man and woman meet on a train in Europe, and wind up spending one evening together in...
By Leonard Pokrovski 2023-03-20 20:33:26 0 23K
Marketing and Advertising
What Skills Are Essential for a CMO?
The role of the Chief Marketing Officer (CMO) is one of the most complex in modern business....
By Dacey Rankins 2025-09-25 15:37:03 0 2K

BigMoney.VIP Powered by Hosting Pokrov