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
How do I attract investors for my business?
Securing investors is a crucial step for many entrepreneurs looking to grow their businesses....
Par Dacey Rankins 2025-03-13 16:39:00 0 16KB
Business
What Is a Social Enterprise?
In today’s world, more businesses are being built not just to generate profit—but to...
Par Dacey Rankins 2025-04-14 13:56:15 0 11KB
Marketing and Advertising
What Is Customer Acquisition Cost (CAC)? How to Calculate It and What Is a Good CAC?
Customer acquisition cost (CAC) is one of the most important metrics in marketing, sales, and...
Par Dacey Rankins 2026-01-16 16:26:45 0 2KB
Business
What Is Influence?
A Complete Guide to Understanding Influence in Psychology, Leadership, Communication, and...
Par Dacey Rankins 2025-11-25 10:53:36 0 2KB
Marketing and Advertising
Why Is Customer Experience Important?
Customer Experience (CX) has evolved from a support function into a strategic growth driver. In...
Par Dacey Rankins 2026-03-04 18:50:35 0 1KB

BigMoney.VIP Powered by Hosting Pokrov