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.

Pesquisar
Categorias
Leia mais
Money
What is Negative Equity?
What is Negative Equity? Negative equity is a financial situation where the value of an asset,...
Por Leonard Pokrovski 2025-09-27 17:35:48 0 5KB
Business
How Does Growth Hacking Relate to Growth Marketing?
Over the last decade, the terms growth hacking and growth marketing have become common in...
Por Dacey Rankins 2025-09-12 16:26:10 0 4KB
Financial Services
Interpreting the aggregate demand/aggregate supply model
Key points The aggregate demand/aggregate supply model is a model that...
Por Mark Lorenzo 2023-03-08 17:45:51 0 19KB
Business
What Skills Are Essential for a Product Manager?
Product management requires a unique blend of technical, strategic, and interpersonal skills....
Por Dacey Rankins 2025-08-15 20:32:44 0 3KB
Business
What Is Mentoring?
Mentoring is a powerful, time-tested approach to personal and professional development. At its...
Por Dacey Rankins 2025-07-18 17:25:02 0 4KB

BigMoney.VIP Powered by Hosting Pokrov