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.

Buscar
Categorías
Read More
Телевидение
Прямой эфир АСТВ. ТВ онлайн. Сахалин.
Официальный канал медиахолдинга «АСТВ». Круглосуточное теле-вещание. Смотрите...
By Nikolai Pokryshkin 2022-10-17 11:54:27 0 28K
Roleplaying
What are newbies afraid of in tabletop RPGs?
Let's start breaking down the fun I'm willing to bet you've definitely heard of tabletop...
By Dacey Rankins 2024-04-08 18:46:28 0 14K
Personal Development
What are the best personal development books?
Personal development is a lifelong process of improving your mindset, skills, habits, and overall...
By Michael Pokrovski 2026-02-03 17:58:09 0 1K
Personal Finance
When Should I Consider Refinancing My Mortgage?
When Should I Consider Refinancing My Mortgage? Should I Refinance if Rates Drop? What Are the...
By Leonard Pokrovski 2025-11-17 17:53:52 0 4K
Marketing and Advertising
Top Tools Every Copywriter Should Use to Improve Their Writing
Introduction: Why Tools Matter in Copywriting Copywriting is often thought of as a purely...
By Dacey Rankins 2025-10-01 14:41:46 0 4K

BigMoney.VIP Powered by Hosting Pokrov