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
Business
What Is a Business Biography?
A business biography is a detailed account of a person’s career and professional...
Par Dacey Rankins 2024-12-27 15:56:10 0 18KB
Социальные проблемы
Неистовый. Unhinged. (2020)
Мать-одиночка Рейчел находится в процессе развода, и этот день у неё не задался. Она проспала и,...
Par Nikolai Pokryshkin 2022-10-16 17:51:15 0 31KB
Softball
Softball: A Celebration of Skill, Strategy, and Team Spirit
Softball: A Celebration of Skill, Strategy, and Team Spirit Softball, a dynamic sport born from...
Par Leonard Pokrovski 2024-07-10 09:15:48 0 23KB
Marketing and Advertising
How Does Radio Advertising Work?
Radio advertising has remained a powerful marketing channel for decades because of its ability to...
Par Dacey Rankins 2026-02-06 19:17:05 0 555
Marketing and Advertising
How Long Does SEM Take to Work?
One of the most common questions businesses ask before launching search engine marketing (SEM)...
Par Dacey Rankins 2026-02-19 17:29:56 0 3KB

BigMoney.VIP Powered by Hosting Pokrov