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
Major Companies
Major Companies: Pillars of the Global Economy
In the interconnected world of the 21st century, certain companies have emerged as titans,...
Par Dacey Rankins 2024-11-15 15:00:01 0 15KB
Жизненные вопросы
Жить. Ikiru. (1952)
Старик узнаёт, что жить ему осталось недолго и решает, что детская площадка на месте зловонного...
Par Nikolai Pokryshkin 2023-01-19 11:36:02 0 29KB
Business
How Can I Align Individual Purpose with Team Objectives?
In today’s dynamic workplace, aligning individual purpose with team objectives is not just...
Par Dacey Rankins 2025-07-17 14:25:54 0 9KB
Economics
What Is Opportunity Cost?
What Is Opportunity Cost? Every choice we make comes with a trade-off. When you decide how to...
Par Leonard Pokrovski 2026-01-27 18:49:46 0 2KB
Animation
The Art of Animation: A Journey Through Motion and Imagination
Animation has evolved from simple sketches to complex digital masterpieces, captivating audiences...
Par Dacey Rankins 2024-10-17 17:39:28 0 24KB

BigMoney.VIP Powered by Hosting Pokrov