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.

Cerca
Categorie
Leggi tutto
Decision Making and Problem Solving
Can screen time affect memory?
The mind is not a warehouse, and it is certainly not a hard drive. We operate under the...
By Michael Pokrovski 2026-07-12 21:59:41 0 950
Business
What Is the Best Niche for a Membership Business?
It's one of the most common questions aspiring membership entrepreneurs ask. "What's the best...
By Dacey Rankins 2026-06-26 20:14:03 0 2K
Социальные проблемы
Новые времена. Modern Times. (1936)
«Новые времена» - это эмоциональный, выдержанный в комическом ключе, отклик на...
By Nikolai Pokryshkin 2022-12-06 22:41:12 0 32K
Business
What Should a Presentation Include?
A well-structured presentation isn’t just about attractive slides — it’s about...
By Dacey Rankins 2025-12-04 16:24:25 0 6K
Hand-Eye Coordination
Modern children sit a lot:
Modern children sit a lot: in high chairs, car seats, strollers, at cartoons, tablets, lessons....
By Leonard Pokrovski 2024-04-04 21:08:04 0 20K

BigMoney.VIP Powered by Hosting Pokrov