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
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 10KB
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 21KB
Business
How Do You Balance Innovation with Market Demand?
Innovation is often described as the heartbeat of business success, but it doesn’t occur in...
Par Dacey Rankins 2025-08-19 17:53:08 0 3KB
Business
What Are Some Famous Startup Success Stories?
Every billion-dollar company once started as an idea—sometimes in a dorm room, a garage, or...
Par Dacey Rankins 2025-04-11 17:02:14 0 8KB
Business
Why is Business Strategy Important? Understanding the Role of Strategy in Achieving Business Goals
Business strategy is not just a buzzword; it’s the foundation of any successful business. A...
Par Dacey Rankins 2024-12-23 16:02:49 0 10KB

BigMoney.VIP Powered by Hosting Pokrov