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.

Pesquisar
Categorias
Leia Mais
Programming
JS Error Handling
JavaScript supports a compact set of statements, specifically control flow statements, that you...
Por Jesse Thomas 2023-06-05 19:42:11 0 12K
Economics
How Is Econometrics Used in Economic Forecasting?
How Is Econometrics Used in Economic Forecasting? Economic forecasting is a critical tool for...
Por Leonard Pokrovski 2026-03-25 07:14:25 0 7K
Arts, Culture and Entertainment
Coco. (2017)
Aspiring musician Miguel, confronted with his family's ancestral ban on music, enters the Land of...
Por Leonard Pokrovski 2022-12-30 21:46:57 0 32K
Business
What Are Segments?
In Google Analytics, raw data can sometimes be overwhelming. Looking at total users, sessions, or...
Por Dacey Rankins 2025-08-30 00:51:53 0 11K
Social Issues
The Intern. (2015)
Seventy-year-old widower Ben Whittaker has discovered that retirement isn't all it's cracked up...
Por Leonard Pokrovski 2023-05-25 18:58:50 0 26K

BigMoney.VIP Powered by Hosting Pokrov