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
Should I Get PMP or Other Certification? FDA Advice, Career Transitions, and Education FAQs
For professionals in project-driven roles—especially those working in regulated...
Par Dacey Rankins 2025-07-15 16:04:08 0 7KB
Социальные проблемы
Антебеллум. Antebellum. (2020)
Известная писательница, активистка и борец за права чёрных женщин Вероника Хенли оказывается в...
Par Nikolai Pokryshkin 2022-10-14 21:00:31 0 42KB
Marketing and Advertising
How to Reduce Email Unsubscribes (and Keep Subscribers Engaged)
Introduction Every email marketer has experienced it: after putting effort into creating a...
Par Dacey Rankins 2025-10-03 20:41:19 0 4KB
Business
How Is User Behavior in Search Different From General Behavior on Websites?
When people browse the internet, their actions can be categorized into two broad contexts:...
Par Dacey Rankins 2025-08-25 17:13:33 0 5KB
Business
What Do I Do After Collecting Feedback?
Collecting feedback is just the first step in improving products, services, or experiences. The...
Par Dacey Rankins 2025-07-30 11:27:28 0 6KB

BigMoney.VIP Powered by Hosting Pokrov