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
Business
What Are Common Conflict Styles?
Conflict is a natural part of human interaction, but people respond to it in very different ways....
By Dacey Rankins 2025-06-05 14:21:05 0 10K
Business
Who Handles Shipping on Marketplaces?
The sale is the exciting part. The shipping is the revealing part. A customer clicks "Buy Now."...
By Dacey Rankins 2026-06-18 14:53:48 0 3K
Productivity
How do I avoid burnout while staying productive?
How Do I Avoid Burnout While Staying Productive? Burnout occurs when chronic stress outpaces...
By Michael Pokrovski 2026-02-19 20:26:23 0 18K
Business
How to reduce workplace inefficiency?
It wasn’t a major failure. A document needed approval. It moved from one desk—then...
By Dacey Rankins 2026-04-30 17:44:58 0 5K
Business
What Are the Risks of Using PaaS?
The Risk Wasn't the Platform. It Was the Assumption. I once joined a post-launch review for a...
By Dacey Rankins 2026-07-06 08:38:52 0 2K

BigMoney.VIP Powered by Hosting Pokrov