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.

Search
Categories
Read More
Crime
Understanding Crime: Exploring Causes, Consequences, and Paths to Prevention
Understanding Crime: Exploring Causes, Consequences, and Paths to Prevention Introduction:Crime...
By Leonard Pokrovski 2024-06-06 21:25:12 0 20K
Magazines and E-zines
Top 10 Magazines of the week
Frieze Frieze Magazine Website The respected British art magazine celebrated its 20th...
By FWhoop Xelqua 2023-07-06 20:29:53 0 22K
Marketing and Advertising
How Long Should Each Podcast Episode Be?
Introduction: Why Length Matters More Than You Think One of the most debated questions among new...
By Dacey Rankins 2025-10-22 15:47:46 0 1K
Business
What Are the Key Phases of a Project?
Project management is a structured approach to planning and guiding project processes from start...
By Dacey Rankins 2025-05-15 14:32:54 0 7K
Employment
The Evolving Landscape of Employment in the Modern World
Employment is a fundamental aspect of modern society, shaping the economy, providing individuals...
By Dacey Rankins 2025-01-23 15:12:11 0 12K

BigMoney.VIP Powered by Hosting Pokrov