JavaScript set()

0
11K

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.

Site içinde arama yapın
Kategoriler
Read More
Programming
Python Newline
In Python, the new line character “\n” is used to create a new line....
By Jesse Thomas 2023-03-01 22:08:02 0 12K
Business
What Are the Key Responsibilities of a Startup CEO?
  The role of a startup CEO is both exhilarating and daunting. Unlike in mature companies...
By Dacey Rankins 2025-05-01 16:21:54 0 10K
Marketing and Advertising
How Do I Set Up a Remarketing Campaign? A Step-by-Step Guide for Beginners and Professionals
Remarketing is one of the most effective ways to turn website visitors into paying customers....
By Dacey Rankins 2026-02-11 23:59:15 0 2K
Economics
What industries were most affected by Brexit?
What industries were most affected by Brexit?Finance, agriculture, fishing, and manufacturing...
By Leonard Pokrovski 2026-02-04 21:13:27 0 1K
Business
What Is the Sales Funnel?
The sales funnel is one of the most important concepts in sales and marketing. Almost every sales...
By Dacey Rankins 2025-12-16 20:13:38 0 1K

BigMoney.VIP Powered by Hosting Pokrov