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
Social Issues
The Usual Suspects. (1995),
A sole survivor tells of the twisty events leading up to a horrific gun battle on a boat, which...
Par Leonard Pokrovski 2022-12-02 20:31:54 0 24KB
Financial Services
The economics of pollution
Key Points Economic production can cause environmental damage. This tradeoff...
Par Mark Lorenzo 2023-01-31 15:07:38 0 14KB
Business
What Is Your Leadership Style?
Leadership is not a one-size-fits-all approach. Every leader brings a unique set of values,...
Par Dacey Rankins 2025-05-07 14:10:43 0 10KB
Marketing and Advertising
What Is Earned Media?
Understanding Its Value, Role, and Impact in Modern Public Relations In the landscape of modern...
Par Dacey Rankins 2025-10-30 16:32:11 0 5KB
Personal Finance
How Much Should I Save for Retirement — and When Can I Retire?
How Much Should I Save for Retirement — and When Can I Retire? A Practical Guide to...
Par Leonard Pokrovski 2025-11-18 21:11:55 0 4KB

BigMoney.VIP Powered by Hosting Pokrov