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
Economics
What is the difference between comparative economics and political economy?
What is the difference between comparative economics and political economy? Students often mix...
By Leonard Pokrovski 2026-03-02 19:59:23 0 2K
Marketing and Advertising
How Do I Target the Right Audience for User Acquisition?
Targeting the right audience is the foundation of successful user acquisition. You can have...
By Dacey Rankins 2026-02-26 16:56:43 0 1K
Marketing and Advertising
What Is Advertising?
Advertising is everywhere — on websites, social media, billboards, TV, search engines,...
By Dacey Rankins 2025-12-27 03:27:53 0 3K
Business
What Is a Minimum Viable Product (MVP)?
A Minimum Viable Product (MVP) is the simplest version of a product that allows a team to collect...
By Dacey Rankins 2025-03-07 13:49:31 0 11K
Information Services
Understanding Business Information Services: Empowering Data-Driven Decision-Making
In today’s fast-paced business environment, data is one of the most valuable assets a...
By Dacey Rankins 2024-11-19 18:24:42 0 14K

BigMoney.VIP Powered by Hosting Pokrov