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
Finance
What Moves Exchange Rates?
What Moves Exchange Rates? The Forces Behind Currency Fluctuations Exchange rates — the...
Par Leonard Pokrovski 2025-10-16 19:13:47 0 5KB
Television
Josh Mcdowell. Live TV. USA
See Josh McDowell full list of movies and tv shows from their career. Christian counseling...
Par Nikolai Pokryshkin 2022-10-05 08:10:52 0 45KB
Systems
Computers - How they have come to be, and what they are
Computer (English computer, IPA: [kəmˈpjuː.tə(ɹ)]- "calculator", from the Latin computare - to...
Par FWhoop Xelqua 2023-02-16 15:33:57 0 22KB
Personal Finance
Are Pensions and Retirement Benefits Taxable? A Comprehensive Guide
Are Pensions and Retirement Benefits Taxable? A Comprehensive Guide Planning for retirement...
Par Leonard Pokrovski 2025-11-26 20:23:16 0 8KB
Antiques and Collectibles
A Guide to Shopping for Antiques and Collectibles: Tips for Finding Timeless Treasures
Shopping for antiques and collectibles is a thrilling journey that combines history, artistry,...
Par Dacey Rankins 2024-11-29 15:08:02 0 12KB

BigMoney.VIP Powered by Hosting Pokrov