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
Mental Health
ADHD: Prognosis
ADHD persists into adulthood in about 30–50% of cases. Those affected are likely to develop...
By Kelsey Rodriguez 2023-05-05 15:34:28 0 11K
Marketing and Advertising
How Long Does AdSense Approval Take?
One of the most common questions from new publishers is simple but frustrating: “How long...
By Dacey Rankins 2025-12-24 20:31:14 0 2K
История
Хороший, плохой, злой. The Good, the Bad and the Ugly. (1966)
В разгар гражданской войны таинственный стрелок скитается по просторам Дикого Запада. У него нет...
By Nikolai Pokryshkin 2022-11-05 20:26:12 0 36K
FAQs, Help, and Tutorials
215 Stop Words That Can Cause Your Email to Go to Spam
215 Stop Words That Can Cause Your Email to Go to Spam It is...
By Leonard Pokrovski 2024-03-26 20:59:12 0 35K
Personal Finance
Can I Remove Accurate Negative Information From My Credit Report?
Can I Remove Accurate Negative Information From My Credit Report? When it comes to your credit...
By Leonard Pokrovski 2025-10-27 21:14:14 0 6K

BigMoney.VIP Powered by Hosting Pokrov