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.

Search
Categories
Read More
Human Resources
What Is Executive Search in Headhunting?
In modern organizations, the recruitment of highly qualified leaders is one of the most critical...
By Dacey Rankins 2026-03-16 16:09:21 0 5K
Business
What Are the Best E-Commerce Platforms for Retailers?
Retailers often spend months debating products. They debate pricing. They debate promotions....
By Dacey Rankins 2026-07-20 17:21:24 0 1K
Business
What Should I Include in a Pitch?
A pitch is one of the most important communication tools in business. Whether you’re...
By Dacey Rankins 2025-12-02 18:55:50 0 4K
Personal Finance
Can You Collect Social Security From Two Sources?
Can You Collect Social Security From Two Sources? Understanding Dual Entitlement, Spousal...
By Leonard Pokrovski 2025-12-11 20:10:16 0 14K
Office Products
Office Tools
Choosing office suppliesIs it time to stock up on office stationery? Not sure where to start?...
By FWhoop Xelqua 2023-03-25 16:24:05 0 27K

BigMoney.VIP Powered by Hosting Pokrov