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
Pharmacy
Pharmacy in the USA
Pharmacy in the USA Retail trade in medicines in the USA is a huge market,...
By Leonard Pokrovski 2024-04-20 22:26:44 0 15K
Sport
Kill Bill: Vol. 2. (2004)
The Bride continues her quest of vengeance against her former boss and lover Bill, the reclusive...
By Leonard Pokrovski 2023-02-25 14:40:54 0 21K
Personal Finance
What Is a Credit Report?
What Is a Credit Report? A credit report is a detailed record of your credit history — a...
By Leonard Pokrovski 2025-10-28 16:28:56 0 1K
Rehber
Lawrence of Arabia. (1962)
The story of T.E. Lawrence, the English officer who successfully united and led the diverse,...
By Leonard Pokrovski 2023-01-16 21:49:58 0 20K
Business
How Can Small Businesses Develop an Effective Strategy?
For small businesses, developing an effective strategy is critical to ensuring long-term success...
By Dacey Rankins 2024-12-25 18:33:30 0 19K

BigMoney.VIP Powered by Hosting Pokrov