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
Marketing and Advertising
Why Was My AdSense Account Suspended or Banned?
Few things are more frustrating for a website owner or creator than opening their email and...
Par Dacey Rankins 2025-12-25 20:14:00 0 2KB
Personal Organization
Social work
Social work with the elderly in the UNITED STATES In the United States, the implementation of...
Par FWhoop Xelqua 2023-01-21 17:55:34 0 24KB
Научная фантастика и фэнтези
Райя и последний дракон. Raya and the Last Dragon. (2021)
Давным-давно в волшебной стране Кумандре бок о бок с людьми жили драконы — создатели и...
Par Nikolai Pokryshkin 2022-09-21 18:57:43 0 28KB
Television
TMJ4 Live TV in Milwaukee, Wisconsin, USA
TMJ4 was the first commercial TV station in Wisconsin, hitting the airwaves on December 3, 1947....
Par Nikolai Pokryshkin 2022-10-27 14:39:48 0 31KB
Business
Analytics tools in marketing
Analytics tools in marketing   Analytics is the heart of successful...
Par Leonard Pokrovski 2024-08-23 15:37:58 0 25KB

BigMoney.VIP Powered by Hosting Pokrov