JavaScript set()

0
11K

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.

Site içinde arama yapın
Kategoriler
Read More
Internet
How to become popular on facebook
Popularity on Facebook depends on your ability to create engaging posts that will interest other...
By FWhoop Xelqua 2023-05-29 16:54:07 0 21K
Analysis and Opinion
How to analyze info
How to learn how to analyze information? The modern world is filled with...
By FWhoop Xelqua 2022-12-22 18:53:10 0 21K
Business
How Does a Social Enterprise Differ from a Nonprofit?
In a world where organizations strive to address pressing social and environmental issues, two...
By Dacey Rankins 2025-04-15 21:13:34 0 11K
Hacking
Hacking: The Good, the Bad, and the Ethical
Hacking is a term that evokes a range of emotions and ideas, often linked to cybersecurity...
By Dacey Rankins 2024-11-04 18:09:28 0 11K
Financial Services
Timeline: Paying for college
Background Throughout this section of the resource, we focus on what...
By Mark Lorenzo 2023-06-01 19:13:16 0 15K

BigMoney.VIP Powered by Hosting Pokrov