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.

Site içinde arama yapın
Kategoriler
Read More
Social Issues
Mean Girls. (2004)
Cady Heron is a hit with The Plastics, the A-list girl clique at her new school, until she makes...
By Leonard Pokrovski 2023-05-12 19:42:13 0 32K
Business and Corporate Finance
What Is Break-Even Analysis?
What Is Break-Even Analysis? Break-even analysis is a fundamental financial tool used by...
By Leonard Pokrovski 2026-01-10 10:18:45 0 2K
Business
How Does Remote Work Impact Productivity?
The rise of remote work has transformed the workplace in recent years, prompting both employees...
By Dacey Rankins 2025-01-29 17:04:09 0 13K
Business
How Can I Attract and Retain Talent in My Startup?
Attracting and retaining top talent is one of the most crucial aspects of growing a successful...
By Dacey Rankins 2025-03-04 14:38:37 0 15K
Social Issues
The Departed. (2006)
An undercover cop and a mole in the police attempt to identify each other while infiltrating an...
By Leonard Pokrovski 2022-12-01 20:36:46 0 23K

BigMoney.VIP Powered by Hosting Pokrov