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
Business
What Are the Best Small Business Loans or Grants Available?
Starting or expanding a small business often requires financial support. Fortunately, various...
Par Dacey Rankins 2025-03-14 16:10:16 0 14KB
Environment
Hammerhead Sharks
The hammerhead sharks are a group of sharks that form the family Sphyrnidae, so named for the...
Par FWhoop Xelqua 2023-04-25 18:37:58 0 18KB
Financial Services
Efficiency in perfectly competitive markets
Key points Long-run equilibrium in perfectly competitive markets meets two...
Par Mark Lorenzo 2023-02-15 11:44:02 0 16KB
Personal Finance
Are There Ways to Reduce or Manage Education Costs?
Are There Ways to Reduce or Manage Education Costs? For many students and families, the cost of...
Par Leonard Pokrovski 2025-10-31 20:38:26 0 9KB
Business
How Do I Make Meetings More Engaging?
Moving Beyond Monologues to Meaningful Interaction Let’s face it—many meetings are...
Par Dacey Rankins 2025-07-31 16:55:03 0 8KB

BigMoney.VIP Powered by Hosting Pokrov