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
CBS, WIVB 4 Live TV. Buffalo, New York, United States.
WIVB-TV, (UHF digital channel 39), is a CBS-affiliated television station located in Buffalo, New...
By Nikolai Pokryshkin 2022-11-03 10:26:05 0 47K
Business
How Is the Success of an Influencer Marketing Campaign Measured?
Influencer marketing has become one of the most powerful tools for brands to connect with their...
By Dacey Rankins 2025-02-27 17:07:04 0 16K
Supercomputing
How Studying The Brain Helps Build A Neuromorphic Supercomputer
How Studying The Brain Helps Build A Neuromorphic Supercomputer Australian scientists are...
By Leonard Pokrovski 2024-03-29 22:16:37 0 26K
Other
Silicon Cowboys. (2016)
Three friends dream up the Compaq portable computer at a Texas diner in 1981, and soon find...
By Leonard Pokrovski 2023-06-09 20:27:47 0 37K
Business and Corporate Finance
What Is Financial Modeling in Corporate Finance?
What Is Financial Modeling in Corporate Finance? Excel Modeling and Forecasting Explained...
By Leonard Pokrovski 2026-01-21 19:27:28 0 2K

BigMoney.VIP Powered by Hosting Pokrov