JavaScript set()

0
11KB

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.

Rechercher
Catégories
Lire la suite
Marketing and Advertising
How Much Does YouTube Advertising Cost?
One of the most common questions advertisers ask is: How much does YouTube advertising cost? The...
Par Dacey Rankins 2026-03-02 19:40:48 0 1KB
Life Issues
Naked Weapon. (2002)
40 13 y.o. girls are kidnapped and the next 6 years forcefully trained to be sexy assassins. The...
Par Leonard Pokrovski 2023-05-17 20:27:00 0 32KB
Ужасы
Дальний космос. Stowaway (2021)
Экипаж космического корабля, направляющегося на Марс, обнаруживает на борту случайного пассажира...
Par Nikolai Pokryshkin 2022-09-18 22:13:39 0 65KB
Personal Finance
How Salary Is Calculated: A Practical Guide to Hourly Pay, Salaried Pay, Overtime, and Job Classification
How Salary Is Calculated: A Practical Guide to Hourly Pay, Salaried Pay, Overtime, and Job...
Par Leonard Pokrovski 2025-11-28 21:18:04 0 4KB
Personal Finance
Can I Remove Accurate Negative Information From My Credit Report?
Can I Remove Accurate Negative Information From My Credit Report? When it comes to your credit...
Par Leonard Pokrovski 2025-10-27 21:14:14 0 7KB

BigMoney.VIP Powered by Hosting Pokrov