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.

Zoeken
Categorieën
Read More
Living History
Living History Recreation: Bringing the Past to Life Through Timeless Experience
Living History Recreation: Bringing the Past to Life Through Timeless Experience In the vast...
By Leonard Pokrovski 2024-05-14 23:29:07 0 22K
Television
FOX 9 Live TV, Minneapolis-St. Paul USA
Minneapolis-St. Paul news, Minnesota weather, traffic and sports from FOX 9, serving the Twin...
By Nikolai Pokryshkin 2022-09-21 08:26:12 0 40K
Decision Making and Problem Solving
How do psychologists study biases?
How Do Psychologists Study Biases? Imagine a researcher walks into a classroom and asks a simple...
By Michael Pokrovski 2026-06-11 12:13:48 0 1K
Financial Services
Types of market-oriented environmental tools
Key points The three main categories of market-oriented environmental policies are...
By Mark Lorenzo 2023-02-07 17:15:56 0 13K
Productivity
What is task management?
Task management is a fundamental concept in productivity, project management, and personal...
By Michael Pokrovski 2026-03-09 21:33:44 0 5K

BigMoney.VIP Powered by Hosting Pokrov