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.

Buscar
Categorías
Read More
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 53K
Business
How Can I Tell If a User Story Is Too Large?
In Agile development, user stories are designed to be small, actionable units of work that...
By Dacey Rankins 2025-08-25 18:37:49 0 11K
Marketing and Advertising
What Is Customer Acquisition for B2B vs B2C?
Customer acquisition looks very different depending on whether a business sells to other...
By Dacey Rankins 2026-01-19 15:19:29 0 2K
Internet
How to Get More Views on SoundCloud in 2023
SoundCloud has long been a haven for artists and musicians. Despite the fact that the landscape...
By FWhoop Xelqua 2023-06-01 20:12:00 0 25K
Programming
Discord.py vs Nextcord
When making a bot on Discord, you've probably ran into 2 libraries that looks really similar. In...
By Jesse Thomas 2023-02-07 21:56:18 0 12K

BigMoney.VIP Powered by Hosting Pokrov