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.

Search
Categories
Read More
Small Business
How to open a business in the United States as a foreigner remotely
How to open a business in the United States as a foreigner remotely If you...
By Leonard Pokrovski 2024-03-18 19:13:10 0 28K
Algorithms
Understanding Computer Algorithms: The Heart of Modern Computing
In the world of computing, algorithms are the unseen engines that drive software, applications,...
By Dacey Rankins 2025-01-24 18:27:51 0 13K
Business
What Inspired This Leader to Pursue Their Career?
Behind every successful leader lies a story — one shaped by experiences, passions, and a...
By Dacey Rankins 2025-06-26 16:48:53 0 5K
Природа и наука
Король Лев. The Lion King. (1994)
У величественного Короля-Льва Муфасы рождается наследник по имени Симба. Уже в детстве...
By Nikolai Pokryshkin 2022-11-27 22:00:17 0 37K
Marketing and Advertising
How Much Will Video Marketing Cost — and How Should You Budget for It?
Introduction Video marketing has become an essential part of modern brand strategy. Whether...
By Dacey Rankins 2025-11-10 16:34:03 0 947

BigMoney.VIP Powered by Hosting Pokrov