JavaScript set()

0
6Кб

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.

Поиск
Категории
Больше
E-Commerce
E-commerce
E-commerce is an area of the digital economy that includes all financial and trade transactions...
От FWhoop Xelqua 2023-07-27 18:12:03 0 25Кб
Transportation and Logistics
Transport logistics
  Transport logistics is one of the most important elements in the organization...
От FWhoop Xelqua 2022-10-13 15:26:42 0 16Кб
Business
How Can I Attract and Retain Talent in My Startup?
Attracting and retaining top talent is one of the most crucial aspects of growing a successful...
От Dacey Rankins 2025-03-04 14:38:37 0 2Кб
Programming
Tips to Avoid Ratelimits
Ratelimits can happen when you send a request too many times in a certain amount of time. To...
От Jesse Thomas 2023-02-16 22:02:39 0 6Кб
Business
Direct Selling
Why is this type of trading so popular in different parts of the world? Below we have compiled...
От Dacey Rankins 2024-09-11 13:36:47 0 7Кб
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov