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
Programming
Reddit API Pricing
Reddit began shutting down some third-party apps after weeks of outrage over its API pricing...
Par Jesse Thomas 2023-07-10 23:45:44 0 14KB
Business
How Did They Climb to the Top—Career Path and Pivotal Roles?
Every leader’s journey to the top is unique, but most share common themes of persistence,...
Par Dacey Rankins 2025-08-14 21:37:01 0 7KB
Социальные проблемы
Жизнь других. The Lives of Others. (2006)
Сто тысяч сотрудников, двести тысяч информаторов. Вместе они — щит и меч партии, знаменитое...
Par Nikolai Pokryshkin 2022-12-15 11:29:22 0 32KB
Customer Service
What Is an Example of Good Customer Experience?
Understanding what constitutes a good customer experience (CX) is essential for businesses that...
Par Dacey Rankins 2026-03-06 19:03:03 0 2KB
Business
100 One-on-One Questions for Managers and Employees
One-on-one meetings are among the most valuable tools for building trust, improving performance,...
Par Dacey Rankins 2025-06-03 13:38:14 0 18KB

BigMoney.VIP Powered by Hosting Pokrov