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
Why Is User Retention Important?
In business, growth often gets equated with acquisition—bringing in as many new customers...
Par Dacey Rankins 2025-09-16 16:23:09 0 3KB
Domicile
How to Make Your Home Cozier, Designer's Tips
How to Make Your Home Cozier, Designer's Tips Cozy home design is an interior decoration...
Par Leonard Pokrovski 2024-04-24 16:07:48 0 18KB
Business
Whose Life Have I Impacted?
One of the most powerful questions you can ask yourself is: Whose life have I impacted? At first,...
Par Dacey Rankins 2025-09-09 14:18:31 0 3KB
Economics
What Is Consumer Capitalism?
What Is Consumer Capitalism?Consumption and Culture Consumer capitalism is an economic and...
Par Leonard Pokrovski 2026-02-12 21:49:52 0 1KB
Business
Psychology of Brand Perception: How Colors, Fonts, and Images Affect Customers
Why do we buy clothes from the same brands, consider coffee in a certain chain of coffee shops to...
Par Dacey Rankins 2024-09-04 15:42:37 0 13KB

BigMoney.VIP Powered by Hosting Pokrov