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.

Buscar
Categorías
Read More
Business and Corporate Finance
What Is Financial Risk in Business?
What Is Financial Risk in Business? Financial Risk and Risk Management Strategies Introduction...
By Leonard Pokrovski 2026-01-10 10:24:38 0 6K
Productivity
How to stay motivated every day?
How to Stay Motivated Every Day? Most people approach motivation the wrong way. They treat it...
By Michael Pokrovski 2026-05-25 00:09:44 0 1K
Productivity
What is short-term vs long-term goals?
The difference between short-term and long-term goals is often framed as a matter of time, but...
By Michael Pokrovski 2026-04-29 21:40:45 0 1K
Business
What are the Steps in the Customer Development Process?
The Customer Development process is a strategic framework used by entrepreneurs, startups, and...
By Dacey Rankins 2025-03-25 15:36:40 0 14K
Economics
Income inequality case studies
Income Inequality Case Studies Introduction Income inequality refers to the uneven distribution...
By Leonard Pokrovski 2026-07-08 18:23:58 0 641

BigMoney.VIP Powered by Hosting Pokrov