JavaScript set()

0
11KB

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
Business
What Analytics Tools Can Help Track and Enhance My Performance?
In today’s fast-paced digital world, understanding your performance is key to improving and...
Par Dacey Rankins 2025-02-20 16:25:27 0 15KB
Marketing and Advertising
What Happens If My Website Traffic or Ranking Has Dropped?
A Complete Guide to Diagnosing and Recovering from SEO Declines Every website experiences...
Par Dacey Rankins 2025-10-31 15:47:56 0 11KB
Business
What’s the Product Management Career Path and Qualifications?
Product management has emerged as one of the most dynamic and in-demand career paths in the...
Par Dacey Rankins 2025-08-18 13:26:02 0 5KB
Социальные проблемы
Шесть минут до полуночи. Six Minutes to Midnight. (2020)
Англия. Канун Второй мировой войны. Томас Миллер, учитель английского языка в Союзе немецких...
Par Nikolai Pokryshkin 2022-10-08 21:01:36 0 25KB
FAQs, Help, and Tutorials
How to Make Money as a Programmer: Ways to Make Money Writing Code
How to Make Money as a Programmer: Ways to Make Money Writing Code Let's talk about the main...
Par Leonard Pokrovski 2024-03-22 20:28:36 0 31KB

BigMoney.VIP Powered by Hosting Pokrov