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.

Căutare
Categorii
Citeste mai mult
Wrestling
The Thrill and Tradition of Sports Wrestling
Sports wrestling, a centuries-old discipline, continues to captivate athletes and audiences alike...
By Dacey Rankins 2024-07-15 16:41:11 0 17K
Marketing and Advertising
Is AdSense Allowed on Blogs, WordPress, and YouTube?
One of the most common questions beginners ask is whether Google AdSense is allowed on blogs,...
By Dacey Rankins 2025-12-25 20:11:53 0 1K
Productivity
Why is task management important?
Task management is a crucial aspect of productivity, organization, and effective work...
By Michael Pokrovski 2026-03-10 18:12:15 0 4K
Decision Making and Problem Solving
What is “nudge theory”?
What Is “Nudge Theory”? The Fly in the Urinal One of the most famous lessons in...
By Michael Pokrovski 2026-06-10 12:04:19 0 1K
Television
ABC 27 News Live Stream TV. WHTM-TV. Harrisburg, Pennsylvania, USA.
WHTM-TV is an ABC-affiliated television station servingLancaster, York, Lebanon and Harrisburg,...
By Nikolai Pokryshkin 2022-11-02 17:59:13 0 58K

BigMoney.VIP Powered by Hosting Pokrov