JavaScript set()

0
12KB

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
Personal Finance
How Do I Plan a Budget So I Know How Much I Can Afford to Spend?
How Do I Plan a Budget So I Know How Much I Can Afford to Spend? How to Budget First, Then...
Por Leonard Pokrovski 2025-11-13 17:25:49 0 9KB
Business
How Do I Handle Conflicts or Underperformance?
A key challenge every manager faces at some point is addressing team members who are not meeting...
Por Dacey Rankins 2025-07-17 14:34:07 0 5KB
Television
Jesus Live Network. Live TV. USA
Jesus Live Network was created to proclaim Jesus' Word, Purpose, and Power. It is an online...
Por Nikolai Pokryshkin 2022-10-01 11:34:37 0 36KB
Business
What is the Career Path for a COO?
The role of a Chief Operating Officer (COO) is one of the most senior and influential positions...
Por Dacey Rankins 2025-01-28 15:48:28 0 16KB
Horror
The Twilight Saga: Breaking Dawn - Part 2. (2012)
After the birth of Renesmee/Nessie, the Cullens gather other vampire clans in order to protect...
Por Leonard Pokrovski 2023-05-19 20:10:22 0 36KB

BigMoney.VIP Powered by Hosting Pokrov