JavaScript set()

0
12K

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
Television
Eternal Word Television Network (EWTN). Live TV.
Catholic Television by providing quality programming faithful to the teachings of the Catholic...
By Nikolai Pokryshkin 2022-09-29 19:55:16 0 54K
Finance
How Secure Are Fintech Apps and Services?
How Secure Are Fintech Apps and Services? Understanding How Your Data and Transactions Are...
By Leonard Pokrovski 2025-10-13 17:08:15 0 14K
Decision Making and Problem Solving
How do I learn faster?
You sit down to absorb a complex, highly technical subject, armed with an open notebook, a cup...
By Michael Pokrovski 2026-07-22 15:11:50 0 252
Business
What Are Common Challenges Startups Face?
Starting a new business is an exciting venture, but it’s also filled with numerous...
By Dacey Rankins 2025-03-04 14:34:12 0 12K
Decision Making and Problem Solving
What is the best memory training app?
The mind is not a warehouse, and it is certainly not a hard drive. We operate under the...
By Michael Pokrovski 2026-07-17 00:51:17 0 512

BigMoney.VIP Powered by Hosting Pokrov