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 Is Logistics in Management Science?
Management of Flow of Goods, Services, or People Using Optimization, Analytics, Scheduling, etc....
Par Dacey Rankins 2025-07-03 18:01:55 0 6KB
Photography
The Art and Science of Photography: Capturing Moments, Telling Stories
Photography is more than just a method for recording visual images; it is an art form, a mode of...
Par Dacey Rankins 2024-11-13 15:21:37 0 11KB
Marketing and Advertising
What Marketing Channels Should We Use? Balancing Digital and Traditional Approaches
Introduction Choosing the right marketing channels is one of the most crucial decisions a...
Par Dacey Rankins 2025-10-16 18:57:23 0 2KB
Mental Health
Psychosis: Culture
Cross-cultural studies into schizophrenia have found that individual experiences of psychosis and...
Par Kelsey Rodriguez 2023-06-09 20:02:28 0 11KB
Business
How Many Questions Are Too Many?
When gathering feedback—whether through a survey, interview, or conversation—asking...
Par Dacey Rankins 2025-07-28 14:55:05 0 5KB

BigMoney.VIP Powered by Hosting Pokrov