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.

Cerca
Categorie
Leggi tutto
Economics
How do countries balance growth and conservation?
How Do Countries Balance Growth and Conservation? There is a photograph I return to every few...
By Leonard Pokrovski 2026-05-27 00:27:30 0 8K
Tile Games
Tile Games: The Fascinating World of Strategy and Fun
Tile games have captivated players of all ages for centuries, combining elements of strategy,...
By Dacey Rankins 2024-11-20 18:30:58 0 13K
Business
Why Has the On-Demand Economy Grown So Rapidly?
A few decades ago, convenience was considered a bonus. Today, it is often the starting point....
By Dacey Rankins 2026-06-29 06:22:49 0 373
Business
How Can a Solopreneur Effectively Manage Their Time and Tasks?
Time management is one of the most crucial skills for any solopreneur. When you're running a...
By Dacey Rankins 2025-02-12 15:35:48 0 22K
Business
Do I Need Email Marketing?
For a long time, I treated email marketing like a retirement plan. Important eventually.Optional...
By Dacey Rankins 2026-05-18 12:46:41 0 4K

BigMoney.VIP Powered by Hosting Pokrov