JavaScript set()

0
11Кб

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.

Поиск
Категории
Больше
Marketing and Advertising
Many Campaigns Fail Because Inbound Volume Is Low or the Buyer Doesn’t Convert—Does Marketing Even Work for B2B?
In B2B marketing, it’s not uncommon for leaders to question the value of campaigns. Maybe...
От Dacey Rankins 2025-09-20 15:53:40 0 3Кб
Informal Sports
Exploring the Charm and Benefits of Informal Sports
  Exploring the Charm and Benefits of Informal Sports...
От Leonard Pokrovski 2024-06-27 16:37:16 0 20Кб
Business
How Do Startup Founders Handle Taxes?
Handling taxes is a crucial aspect of running a startup, but it can often be confusing and...
От Dacey Rankins 2025-04-04 14:19:29 0 11Кб
Ужасы
Нечестивые. The Unholy. (2021)
На дереве, где 150 лет назад была повешена и сожжена обвинённая в колдовстве, глухонемая...
От Nikolai Pokryshkin 2022-09-21 10:56:12 0 36Кб
Marketing and Advertising
Best Platforms for User-Generated Content (UGC): Where Authentic Voices Thrive
Introduction User-Generated Content (UGC) is only as powerful as the platforms that host and...
От Dacey Rankins 2025-11-07 18:29:58 0 2Кб

BigMoney.VIP Powered by Hosting Pokrov