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
Personal Finance
What Are My Biggest Spending Categories?
What Are My Biggest Spending Categories? Understanding where your money goes each month is one...
By Leonard Pokrovski 2025-11-13 17:29:26 0 5K
Financial Services
The effect of time on your retirement account
The effect of time on your retirement account When you’re young,...
By Mark Lorenzo 2023-05-26 18:53:26 0 13K
Business
What is a marketing strategy and how to develop one
A business without a marketing strategy operates blindly, losing money and time. A company may...
By Dacey Rankins 2024-09-05 18:08:17 0 11K
Netball
The Thrill of Netball: A Sport Bridging Skill and Strategy
The Thrill of Netball: A Sport Bridging Skill and Strategy Netball, a dynamic and fast-paced...
By Leonard Pokrovski 2024-07-03 00:11:56 0 26K
Sport
Creed II. (2018)
Under the tutelage of Rocky Balboa, newly crowned heavyweight champion Adonis Creed faces off...
By Leonard Pokrovski 2023-05-05 20:18:11 0 42K

BigMoney.VIP Powered by Hosting Pokrov