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 Conflict Resolution and Why Is It Important?
Conflict resolution refers to the methods and processes involved in facilitating the peaceful...
Par Dacey Rankins 2025-08-04 18:25:29 0 3KB
Business
Who Should Be Invited to the Meeting?
Why Selecting the Right Participants Drives Better Results Meetings are an essential part of...
Par Dacey Rankins 2025-05-30 14:54:21 0 8KB
Ask an Expert
10 mistakes that will kill your expert video
Many experts make these mistakes and lose their audience. But they are easy to avoid - I have put...
Par FWhoop Xelqua 2022-10-02 12:01:16 0 44KB
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
Marketing and Advertising
How Do I Set Up Retargeting Ads? A Step-by-Step Guide for Beginners and Professionals
Retargeting ads allow businesses to reconnect with people who have already shown interest in...
Par Dacey Rankins 2026-02-13 20:01:40 0 431

BigMoney.VIP Powered by Hosting Pokrov