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.

Buscar
Categorías
Read More
Business
What Tools and Resources Do Competitors Utilize?
In today’s competitive landscape, companies rely on an array of tools, platforms, and...
By Dacey Rankins 2025-09-29 20:09:18 0 6K
Business
What Are the Benefits of Mentoring for the Mentor?
Mentoring is often viewed through the lens of how it benefits the mentee, but the advantages for...
By Dacey Rankins 2025-05-26 14:01:34 0 6K
Business
What Universal Concerns Exist Around Growth?
Growth is a concept that stretches across every level of existence—from the individual to...
By Dacey Rankins 2025-09-09 14:27:01 0 4K
Financial Services
What are market-oriented environmental tools?
Key points The three main categories of market-oriented environmental policies are...
By Mark Lorenzo 2023-05-02 20:21:19 0 15K
Programming
JS Error Handling
JavaScript supports a compact set of statements, specifically control flow statements, that you...
By Jesse Thomas 2023-06-05 19:42:11 0 11K

BigMoney.VIP Powered by Hosting Pokrov