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
Financial Services
How well GDP measures the well-being of society
Key points GDP is an indicator of a society’s standard of living, but it is...
By Mark Lorenzo 2023-06-07 19:57:35 0 13K
Business
What Is Bounce Rate?
In digital marketing and website optimization, one of the most frequently discussed metrics is...
By Dacey Rankins 2025-08-28 17:43:39 0 7K
Small Business
How to Grow Your Small Business: Key Strategies for Success
Running a small business is a remarkable achievement, but growing it into a larger, sustainable...
By Dacey Rankins 2024-11-13 16:32:44 0 15K
Business
What Role Do Tactics Like Deception or Bluffing Play in Negotiation?
Introduction: Understanding Tactics in Negotiation Negotiation is often portrayed as a...
By Dacey Rankins 2025-11-28 14:44:54 0 1K
Financial Services
How perfectly competitive firms make output decisions
Key points As a perfectly competitive firm produces a greater quantity of output,...
By Mark Lorenzo 2023-07-19 22:25:39 0 14K

BigMoney.VIP Powered by Hosting Pokrov