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.

Site içinde arama yapın
Kategoriler
Read More
Financial Services
The expenditure-output, or Keynesian cross, model
Key points The expenditure-output model, or Keynesian cross diagram, shows how the...
By Mark Lorenzo 2023-06-14 20:21:07 0 12K
История
Лоуренс Аравийский. Lawrence of Arabia. (1962)
Фильм рассказывает о легендарном герое Первой мировой войны - английском разведчике Т.Э....
By Nikolai Pokryshkin 2023-01-15 11:03:53 0 24K
Marketing and Advertising
When to Stop Researching and Start Making Decisions: How to Know When You Have Enough Data
Introduction You’ve run surveys, analyzed competitors, interviewed customers, and studied...
By Dacey Rankins 2025-10-13 17:44:54 0 1K
Mental Health
Psychosis: Negative symptoms and Psychosis in adolescents
Negative symptoms Negative symptoms include reduced emotional expression (flat affect),...
By Kelsey Rodriguez 2023-05-17 15:13:50 0 13K
Crafts
Arts and Crafts
Arts & Crafts was an English art movement of the Victorian era (late nineteenth century),...
By FWhoop Xelqua 2023-07-01 19:15:47 0 22K

BigMoney.VIP Powered by Hosting Pokrov