JavaScript set()

0
12KB

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
Business
"Cindy Alvarez – How to create a product that will be bought. Lean Customer Development Method" Summary Review
I read Cindy Alvarez "A Product That Will Be Bought. Lean Customer Development Method" - about...
Par Dacey Rankins 2024-07-26 12:36:15 0 19KB
Human Resources
Who Pays the Headhunter — the Company or the Candidate?
In modern recruitment systems, organizations often rely on specialized professionals to identify...
Par Dacey Rankins 2026-03-16 14:55:21 0 3KB
Financial Services
What are market-oriented environmental tools?
Key points The three main categories of market-oriented environmental policies are...
Par Mark Lorenzo 2023-07-24 17:50:01 0 17KB
Marketing and Advertising
What Is Email Remarketing? A Complete Guide to Re-Engaging Customers Through Personalized Email Campaigns
  While digital advertising platforms continue to evolve, email remains one of the most...
Par Dacey Rankins 2026-02-12 00:20:12 0 4KB
Life Issues
Shutter Island. (2010)
In 1954, a U.S. Marshal investigates the disappearance of a murderer who escaped from a hospital...
Par Leonard Pokrovski 2023-02-16 13:03:05 1 27KB

BigMoney.VIP Powered by Hosting Pokrov