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
Научная фантастика и фэнтези
Тёмный рыцарь: Возрождение легенды. The Dark Knight Rises. (2012)
Восемь лет назад Бэтмен растворился в ночи, превратившись из героя в беглеца. Приняв на себя вину...
Par Nikolai Pokryshkin 2022-12-25 10:15:19 0 30KB
Marketing and Advertising
How Do We Differentiate Ourselves from Competitors (Unique Selling Proposition / Positioning)?
Introduction In today’s crowded marketplace, standing out is no longer optional —...
Par Dacey Rankins 2025-10-15 19:06:14 0 4KB
Business
Can I Bring Over My Power BI Reports into Fabric?
One of the most common questions business users ask when exploring Microsoft Fabric is:...
Par Dacey Rankins 2025-09-01 09:40:56 0 4KB
Marketing and Advertising
How Do I Handle Negative Comments and Reputation Issues on Social Media?
Social media can be a brand’s best friend — or its biggest challenge. The same...
Par Dacey Rankins 2025-11-04 17:19:38 0 3KB
Life Issues
Addicted. (2014)
A gallerist risks her family and flourishing career when she enters into an affair with a...
Par Leonard Pokrovski 2023-05-21 18:12:36 0 35KB

BigMoney.VIP Powered by Hosting Pokrov