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
Physics
Unraveling the Mysteries of the Universe: A Journey through the World of Physics
Unraveling the Mysteries of the Universe: A Journey through the World of Physics Physics, often...
Par Leonard Pokrovski 2024-05-25 11:04:07 0 26KB
Finance
How Fintech Is Transforming the Finance Industry and Global Economy
How Fintech Is Transforming the Finance Industry and Global Economy Financial...
Par Leonard Pokrovski 2025-10-13 17:00:46 0 5KB
Financial Services
How perfectly competitive firms make output decisions
Key points As a perfectly competitive firm produces a greater quantity of output,...
Par Mark Lorenzo 2023-04-26 17:47:49 0 12KB
Marketing and Advertising
Guerrilla Marketing vs. Viral and Traditional Marketing: What’s the Difference?
Understanding what sets guerrilla tactics apart from viral content and conventional advertising....
Par Dacey Rankins 2025-10-07 17:33:24 0 7KB
Business
Frequency of One-on-One Meetings: Do I Have to Run Weekly 1:1s?
How Often Is Often Enough? One-on-one meetings (1:1s) are among the most important tools for...
Par Dacey Rankins 2025-08-01 14:09:35 0 7KB

BigMoney.VIP Powered by Hosting Pokrov