JavaScript set()

0
8KB

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.

Rechercher
Catégories
Lire la suite
Life Issues
Maleficent. (2014)
A vengeful fairy is driven to curse an infant princess, only to discover that the child may be...
Par Leonard Pokrovski 2023-05-03 20:52:38 0 20KB
Business
Neural networks for business and how to work with them
What are neural networks? Imagine that you need to develop a program that will automatically...
Par Dacey Rankins 2024-09-11 12:23:41 0 8KB
Financial Services
Elasticity in areas other than price
Key points Elasticity is a general term, referring to percentage change of one...
Par Mark Lorenzo 2023-04-14 17:08:27 0 11KB
Middle East
Agriculture and industry in the Middle East
Agriculture and industry in the Middle EastFrom the characteristics of the climate and relief, it...
Par FWhoop Xelqua 2023-03-18 18:51:45 0 23KB
Mental Health
Psychosis: Neuroimaging
The first brain image of an individual with psychosis was completed as far back as 1935 using a...
Par Kelsey Rodriguez 2023-06-05 19:02:07 0 9KB
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov