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
Money
Interest Rates
Interest rates are a fundamental aspect of the global economy, influencing everything from...
By Leonard Pokrovski 2025-09-16 18:35:34 0 11K
Television
FOX 2, Live TV. Detroit USA
FOX 2 Detroit's mornings news includes weather and traffic on the 2's plus breaking local,...
By Nikolai Pokryshkin 2022-09-19 09:03:51 0 54K
Business
How Many People Are on the Team and What’s the Structure?
Knowing the size and structure of the team you’re joining is essential for understanding...
By Dacey Rankins 2025-08-09 18:31:59 0 9K
Economics
What careers exist in environmental economics?
What Careers Exist in Environmental Economics? A river does not submit invoices. A forest does...
By Leonard Pokrovski 2026-05-28 21:55:53 0 4K
Economics
What attracts investment to a country?
What Attracts Investment to a Country? The Real Magnet for Capital Is Not What Most Governments...
By Leonard Pokrovski 2026-06-17 20:36:58 0 1K

BigMoney.VIP Powered by Hosting Pokrov