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
How Can I Improve My Voice for Public Speaking?
Your voice is your main instrument when speaking in public. It carries your ideas, expresses...
Par Dacey Rankins 2025-12-11 16:38:23 0 2KB
Business
How Do I Speak Confidently in Front of Others?
Speaking confidently in front of others — whether it’s a class, a small group, or a...
Par Dacey Rankins 2025-12-10 17:40:47 0 2KB
Social Issues
The Thin Blue Line (1988)
A film that successfully argued that a man was wrongly convicted for murder by a corrupt justice...
Par Leonard Pokrovski 2023-07-24 19:35:46 0 30KB
Gaelic
Gaelic: Unveiling the Rich Heritage and Enduring Legacy of Ireland's Indigenous Culture
  Gaelic: Unveiling the Rich Heritage and Enduring Legacy of...
Par Leonard Pokrovski 2024-06-25 16:50:48 0 25KB
Marketing and Advertising
How Do Local Mobile Searches Behave / How to Optimize for “Near Me” Searches
Introduction “Near me” searches have become one of the defining behaviors of the...
Par Dacey Rankins 2025-10-17 15:24:32 0 5KB

BigMoney.VIP Powered by Hosting Pokrov