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
Personal Finance
What Are the Common Tax Credits I Might Qualify For?
What Are the Common Tax Credits I Might Qualify For? How Tax Credits Reduce Your Tax Bill and...
Par Leonard Pokrovski 2025-11-21 19:44:25 0 2KB
Business
What’s the Difference Between User Retention and Churn?
When businesses measure customer health, two metrics consistently come up: retention and churn....
Par Dacey Rankins 2025-09-16 16:33:48 0 3KB
Business
How Do I Find Prospects?
Finding prospects is the foundation of every business that intends to grow. Whether you’re...
Par Dacey Rankins 2025-12-08 18:29:27 0 1KB
Nature and Science
Princess Mononoke. (1997)
On a journey to find the cure for a Tatarigami's curse, Ashitaka finds himself in the middle of a...
Par Leonard Pokrovski 2023-01-04 20:54:21 0 22KB
Internet
Engaging Post Ideas That Will Boost Your Social Media Engagement
In today's digital world, where information literally overwhelms us from all sides, competition...
Par FWhoop Xelqua 2023-06-07 14:36:59 0 22KB

BigMoney.VIP Powered by Hosting Pokrov