JavaScript set()

0
12K

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
Holidays
The Top Celebrated Holidays Around the World
Holidays are an essential part of cultures worldwide, offering people a chance to relax,...
By Dacey Rankins 2024-12-05 14:35:52 0 11K
Economics
How Does Microfinance Support Economic Development?
How Does Microfinance Support Economic Development? Microfinance has emerged as a powerful tool...
By Leonard Pokrovski 2026-04-11 17:48:58 0 1K
Business
What Problem Does Your Product Solve?
Every successful product starts with a problem. In fact, most innovations are not born from a...
By Dacey Rankins 2025-08-18 13:29:51 0 6K
Business
How do I find my first customers?
Every startup starts with zero customers. The first few are the hardest to get—but...
By Dacey Rankins 2025-04-11 16:58:52 0 16K
Business and Corporate Finance
How Do Taxes Affect Business Finances?
How Do Taxes Affect Business Finances? Deductions, Write-Offs, and Compliance Taxes are one of...
By Leonard Pokrovski 2026-01-08 09:15:00 0 7K

BigMoney.VIP Powered by Hosting Pokrov