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.

Site içinde arama yapın
Kategoriler
Read More
Television
Live TV, Channel 2 KWGN-TV in Denver, Colorado, USA
Watch live local news and shows from Channel 2 KWGN-TV in Denver, Colorado. When no broadcasts...
By Nikolai Pokryshkin 2022-10-18 09:21:14 0 30K
Regional
Global Innovation Index 2022
According toWIPO's 2022 Global Innovation Index (GII) report, the world's most innovative...
By FWhoop Xelqua 2022-11-23 14:23:39 0 29K
Business
What Kinds of User Behavior Metrics Matter Most?
Understanding user behavior metrics is critical for businesses that want to optimize websites,...
By Dacey Rankins 2025-08-25 17:05:01 0 6K
Marketing and Advertising
How Does Online Advertising Compare to Traditional Advertising?
Advertising has always been about reaching the right audience with the right message. What has...
By Dacey Rankins 2026-01-29 19:23:18 0 2K
Life Issues
American Beauty. (1999)
A sexually frustrated suburban father has a mid-life crisis after becoming infatuated with his...
By Leonard Pokrovski 2022-12-24 16:49:59 0 29K

BigMoney.VIP Powered by Hosting Pokrov