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
Finance
What Is a Debit Card?
What Is a Debit Card? A debit card is a widely used financial tool that allows individuals to...
By Leonard Pokrovski 2025-10-04 22:03:31 0 3K
Business
How Would You Approach Positioning a New Product for a New Buyer Persona?
Product positioning is one of the most crucial responsibilities of Product Marketing Managers...
By Dacey Rankins 2025-09-15 19:00:21 0 4K
Internet
Ad guides, how to properly set up google ads part 3
Part Two: Google ads Part Two (bigmoney.vip) Google Search Network - Keywords and Queries Key...
By FWhoop Xelqua 2023-02-03 16:34:14 0 22K
Business
What’s the Difference Between User Retention and Churn?
When businesses measure customer health, two metrics consistently come up: retention and churn....
By Dacey Rankins 2025-09-16 16:33:48 0 3K
Business
Managing Attendee Dynamics: Use Frameworks Like RACI to Clarify Roles and Keep Meetings Small
Meetings are essential for collaboration, but without structure, they can become bloated and...
By Dacey Rankins 2025-08-04 18:15:58 0 4K

BigMoney.VIP Powered by Hosting Pokrov