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.

Cerca
Categorie
Leggi tutto
Business
Common Project Management Interview Questions
Project management roles require a combination of leadership, communication, organization, and...
By Dacey Rankins 2025-05-19 14:38:51 0 15K
Flowers
The Best Flowers to Shop For: A Guide to Choosing the Perfect Blooms
Whether you’re looking to brighten your home, celebrate a special occasion, or simply enjoy...
By Dacey Rankins 2024-11-29 15:05:28 0 9K
Телевидение
Телеканал "Продвижение" прямой эфир.
Семейно - развлекательный телеканал «Продвижение» создан в 2014 году. Аудитория...
By Nikolai Pokryshkin 2022-11-26 20:54:38 0 27K
Marketing and Advertising
Can I Use AdSense on Multiple Websites?
One of the most common questions publishers ask after getting approved for Google AdSense...
By Dacey Rankins 2025-12-25 20:06:30 0 701
Business
How Do I Generate B2B Leads?
B2B lead generation is different from B2C. You are not selling to impulse buyers — you are...
By Dacey Rankins 2025-12-17 18:56:05 0 1K

BigMoney.VIP Powered by Hosting Pokrov