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
Marketing and Advertising
How Do I Improve My PPC Quality Score?
Improving your PPC Quality Score is one of the most powerful ways to increase profitability,...
By Dacey Rankins 2026-02-05 18:41:06 0 1K
Science Fiction and Fantasy
Batman Begins. (2005)
After training with his mentor, Batman begins his fight to free crime-ridden Gotham City from...
By Leonard Pokrovski 2023-02-03 16:09:37 0 24K
Marketing and Advertising
How Do Ads Make Money?
Advertising is one of the largest industries in the world, generating hundreds of billions of...
By Dacey Rankins 2026-01-05 23:54:49 0 2K
Marketing and Advertising
How Much Does Advertising Cost?
Advertising budgets explained, platform-by-platform costs, pricing models, ROI expectations, and...
By Dacey Rankins 2026-01-05 23:37:31 0 2K
Business
Do You Really Need 1000 Transactions per Month Before CRO Makes Sense?
A common myth in digital marketing is that Conversion Rate Optimization (CRO) only makes sense...
By Dacey Rankins 2025-09-11 16:23:41 0 7K

BigMoney.VIP Powered by Hosting Pokrov