JavaScript set()

0
11KB

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.

Rechercher
Catégories
Lire la suite
Business
Problems of working with personnel at enterprises and how to solve them
The success of the company depends on employees. An increasing number of managers are aware of...
Par Dacey Rankins 2024-09-20 21:47:02 0 23KB
Personal Finance
How Do I Decide What I Should Spend Money On?
How Do I Decide What I Should Spend Money On? Understanding Needs vs. Wants and Making Smarter...
Par Leonard Pokrovski 2025-11-12 16:26:31 0 3KB
Personal Finance
What Is the Best Type of Savings Account?
What Is the Best Type of Savings Account? Comparing High-Yield Savings Accounts, Money Market...
Par Leonard Pokrovski 2025-12-04 17:17:08 0 2KB
Shopping
The Rise of Online Shopping: A Comprehensive Overview
In recent years, online shopping has transformed the way consumers purchase goods and services....
Par Dacey Rankins 2024-10-10 17:10:13 0 24KB
Business
What is Microsoft Fabric?
The world of analytics and data management has grown increasingly complex. Organizations are...
Par Dacey Rankins 2025-09-01 09:30:29 0 4KB

BigMoney.VIP Powered by Hosting Pokrov