JavaScript set()

0
11χλμ.

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.

Αναζήτηση
Κατηγορίες
Διαβάζω περισσότερα
Социальные проблемы
Похитители велосипедов. The Bicycle Thief. (1948)
Послевоенный Рим, давно неработающий Антонио вне себя от радости - он наконец-то находит работу....
από Nikolai Pokryshkin 2023-02-01 21:36:38 0 26χλμ.
Horror
Twilight. (2008)
When Bella Swan moves to a small town in the Pacific Northwest, she falls in love with Edward...
από Leonard Pokrovski 2023-04-28 19:40:18 0 26χλμ.
Weddings
The Ultimate Guide to Shopping for Your Dream Wedding: Making Every Moment Memorable
Shopping for your wedding can be both exhilarating and overwhelming. From finding the perfect...
από Dacey Rankins 2024-06-05 17:49:35 0 19χλμ.
Business
What Are the Regulatory and Tax Considerations in Mergers and Acquisitions?
Mergers and acquisitions (M&A) are among the most transformative actions a company can take....
από Dacey Rankins 2025-11-27 16:40:42 0 2χλμ.
Marketing and Advertising
Examples of Strong Brand Positioning: Learning from the Best
Introduction: Why Learning from the Greats Matters Brand positioning is one of the most powerful...
από Dacey Rankins 2025-10-24 15:59:37 0 5χλμ.

BigMoney.VIP Powered by Hosting Pokrov