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
Law
The Interplay of Society and Law: Navigating the Complex Dynamics
In the intricate dance between society and law, each step influences the other, shaping the...
By Dacey Rankins 2024-06-10 19:55:49 0 15K
Business
Why Closing Is the Most Important Step in Sales
Introduction Sales has many stages—prospecting, qualifying, presenting, handling...
By Dacey Rankins 2025-11-19 19:37:38 0 1K
Business
What Are the Latest Trends in Entrepreneurship?
The world of entrepreneurship is constantly evolving, driven by technological advancements,...
By Dacey Rankins 2025-03-18 13:53:48 0 9K
Reference
Scientific electronic resources
Scopusis the world's largest unified abstract and scientometric database (citation index), which...
By FWhoop Xelqua 2022-11-15 12:39:30 0 27K
Marketing and Advertising
What Tools and Templates Can Help Build or Organize a Marketing Plan?
Introduction Building a marketing plan from scratch can be a daunting task — especially...
By Dacey Rankins 2025-10-15 18:51:02 0 2K

BigMoney.VIP Powered by Hosting Pokrov