JavaScript set()

0
12K

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
Economics
How Does Comparative Economics Compare Public and Private Ownership?
How Does Comparative Economics Compare Public and Private Ownership? Comparative economics is a...
By Leonard Pokrovski 2026-03-06 20:32:06 0 8K
Chemistry
10 interesting facts about chemistry
For many, weekdays are almost monotonous: home, work, home ... And often this drives people into...
By FWhoop Xelqua 2022-11-24 13:54:18 0 31K
История
Плохой поэт. Il cattivo poeta / The Bad Poet. (2021)
1936 год. Джованни Комини - самый молодой федеральный секретарь национал-фашистсткой партии...
By Nikolai Pokryshkin 2022-09-11 21:01:31 0 31K
Productivity
How to set goals effectively?
To set a goal effectively is to perform a surgical strike on your own future. Most people fail...
By Michael Pokrovski 2026-04-29 22:02:17 0 1K
Productivity
How do I avoid procrastination?
How Do I Avoid Procrastination? Procrastination is not a time management problem. It is an...
By Michael Pokrovski 2026-02-27 20:55:08 0 10K

BigMoney.VIP Powered by Hosting Pokrov