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
Do Mentoring and Coaching Complement Each Other? Absolutely. Here's Why.
In today’s fast-paced world of professional development, both mentoring and coaching have...
Par Dacey Rankins 2025-07-08 13:46:40 0 9KB
Marketing and Advertising
Web Marketing and the Creator Economy: Strategies for Sustainable Growth
Introduction Web marketing has become one of the most powerful forces driving modern business...
Par Dacey Rankins 2025-11-12 15:41:51 0 2KB
Home Improvement
7 Tips to Improve Home Comfort
According to the principles of feng shui, an ancient Eastern practice, it takes very little to...
Par FWhoop Xelqua 2022-09-18 12:26:47 0 37KB
Programming
Python List Slicing
In Python, list slicing is a common practice and it is the most used technique for programmers to...
Par Jesse Thomas 2023-03-08 16:49:21 0 12KB
Marketing and Advertising
How Do I Align Marketing With Sales and Understand the Buyer’s Journey?
One of the most common challenges in B2B marketing is the disconnect between marketing and sales...
Par Dacey Rankins 2025-09-20 15:38:59 0 5KB

BigMoney.VIP Powered by Hosting Pokrov