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
Marketing and Advertising
What Is Online Advertising?
Online advertising has become one of the most powerful and dominant forces in modern marketing....
Par Dacey Rankins 2026-01-27 20:31:29 0 5KB
Autre
Why Americans Feel So Poor? (2023)
this documentary will be showing everything about Inflation, low salary rates, Inflation, bad...
Par Leonard Pokrovski 2023-06-01 19:44:44 0 29KB
Marketing and Advertising
How Do I Reduce User Acquisition Cost?
Reducing user acquisition cost (CAC) is one of the most important levers for sustainable growth....
Par Dacey Rankins 2026-02-27 18:25:47 0 11KB
Human Resources
How Much Does a Headhunter Cost?
Recruiting highly qualified professionals is one of the most important challenges organizations...
Par Dacey Rankins 2026-03-16 14:54:30 0 7KB
Personal Finance
What Are the Common Tax Credits I Might Qualify For?
What Are the Common Tax Credits I Might Qualify For? How Tax Credits Reduce Your Tax Bill and...
Par Leonard Pokrovski 2025-11-21 19:44:25 0 9KB

BigMoney.VIP Powered by Hosting Pokrov