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.

Search
Categories
Read More
Money
What affects my credit rating?
What affects my credit rating? A credit rating is one of the most important financial markers...
By Leonard Pokrovski 2025-09-28 21:41:51 0 5K
Business
Interaction with contractors — how to improve work with a programmer
In general, is it worth trying to improve this interaction with developers at all? Why should the...
By Dacey Rankins 2024-09-18 16:35:51 0 15K
Financial Services
Law of demand
Key points The law of demand states that a higher price leads to a lower...
By Mark Lorenzo 2023-06-26 19:13:43 0 13K
Business
How Can You Adapt Your Product Strategy to Market Changes?
In today’s fast-moving business environment, even the best product strategies can quickly...
By Dacey Rankins 2025-08-21 16:37:56 0 7K
Future
Future
A future is a hypothetical segment of a timeline, a set of events that have not yet occurred, but...
By FWhoop Xelqua 2023-03-02 16:16:45 0 19K

BigMoney.VIP Powered by Hosting Pokrov