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.

Suche
Kategorien
Mehr lesen
Financial Services
Demand and the Determinants of Demand
In a competitive market, demand for and supply of a good or service determine the...
Von Mark Lorenzo 2023-01-26 14:43:23 0 18KB
Business
Who Will I Report To?
Understanding who you will report to is critical for determining how your role fits within the...
Von Dacey Rankins 2025-08-09 18:29:48 0 5KB
Chemistry
10 interesting facts about chemistry
For many, weekdays are almost monotonous: home, work, home ... And often this drives people into...
Von FWhoop Xelqua 2022-11-24 13:54:18 0 29KB
Personal Finance
What Income Is Taxable? A Practical Guide to Common Tax Questions
What Income Is Taxable? A Practical Guide to Common Tax Questions Understanding which types of...
Von Leonard Pokrovski 2025-11-20 17:03:58 0 2KB
Business
How Affiliate Marketing Works: A Guide for Those Who Dive Into the Topic
How Affiliate Marketing Works: A Guide for Those Who Dive Into the Topic Customer...
Von Leonard Pokrovski 2024-08-30 20:06:05 0 28KB

BigMoney.VIP Powered by Hosting Pokrov