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.

Buscar
Categorías
Read More
Programming
Python HTML Content
With Python, you can create a variable with only HTML. You can put it in a variable or just...
By Jesse Thomas 2023-02-10 21:36:34 0 13K
Martial Arts
Martial Arts: The Ancient Discipline That Transcends Time
Martial Arts: The Ancient Discipline That Transcends Time Martial arts, a term that conjures...
By Leonard Pokrovski 2024-07-01 22:10:48 0 26K
Economics
What Is the Difference Between Commerce and Trade?
What Is the Difference Between Commerce and Trade? In everyday conversation, people often use...
By Leonard Pokrovski 2026-02-13 19:10:16 0 937
Personal Finance
What is corporate finance?
Corporate finance is an important part of any business. Thanks to proper financial management, a...
By Dacey Rankins 2024-10-28 19:03:10 0 13K
Business
What Is Cold Calling?
Cold calling is one of the oldest sales methods — and still one of the most...
By Dacey Rankins 2025-12-12 17:05:37 0 2K

BigMoney.VIP Powered by Hosting Pokrov