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 Are the Different Types of Influencers?
In the world of social media and digital marketing, influencers play a crucial role in shaping...
Par Dacey Rankins 2025-02-27 16:37:34 0 11KB
Business
How Do You Create or Develop a Product Strategy?
A well-crafted product strategy is the cornerstone of every successful product. Without it, teams...
Par Dacey Rankins 2025-08-21 16:33:32 0 6KB
Crafts
Exploring the World of Arts and Crafts: A Creative Journey
Arts and crafts have been a cherished part of human culture for centuries, blending creativity...
Par Dacey Rankins 2024-11-22 14:34:26 0 13KB
Социальные проблемы
Свидетель обвинения. Witness for the Prosecution. (1957)
Сэр Уилфрид Робартс - тяжело больной адвокат, которому врачи запретили заниматься уголовными...
Par Nikolai Pokryshkin 2022-12-19 12:01:57 0 24KB
Personal Finance
Are Student Loans Forgiven?
Are Student Loans Forgiven? Student loan forgiveness is one of the most discussed and...
Par Leonard Pokrovski 2025-12-16 21:01:09 0 6KB

BigMoney.VIP Powered by Hosting Pokrov