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.

Buscar
Categorías
Read More
Business
Why Prioritize User Stories in Agile Development?
In Agile development, teams are constantly faced with more work than can be accomplished in a...
By Dacey Rankins 2025-08-26 12:07:41 0 10K
Business
Which forecasting method is best for my business?
In today’s dynamic business environment, forecasting is crucial for effective planning and...
By Dacey Rankins 2025-01-10 14:05:47 0 13K
Business
How do I attract investors for my business?
Securing investors is a crucial step for many entrepreneurs looking to grow their businesses....
By Dacey Rankins 2025-03-13 16:39:00 0 15K
Asia
Asia - Tourist Guide
Asia Asia is very different – beautiful, bright, traditional and shocking....
By FWhoop Xelqua 2023-01-24 15:19:37 0 39K
Business
Do We Really Need to Meet?
Questioning the Necessity of Meetings in the Modern Workplace Meetings have long been a staple...
By Dacey Rankins 2025-07-31 12:11:30 0 9K

BigMoney.VIP Powered by Hosting Pokrov