JavaScript set()

0
12K

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
Economics
What causes economic growth?
What Causes Economic Growth? Economic growth is often described through numbers. A country's...
By Leonard Pokrovski 2026-06-14 17:20:27 0 1K
Marketing and Advertising
What Is Targeted Advertising?
Advertising has always been about reaching the right audience at the right time. In the past,...
By Dacey Rankins 2026-01-06 12:52:17 0 5K
Productivity
How do I declutter my home?
How Do I Declutter My Home? Decluttering your home is more than just tidying up—it is a...
By Michael Pokrovski 2026-04-09 19:54:14 0 6K
Business
What Is a Licensing Fee?
Few business terms sound more straightforward than "licensing fee." A fee is paid. A license is...
By Dacey Rankins 2026-06-13 12:08:33 0 2K
Business
How to Run a Successful Business in Today's Environment
How to Run a Successful Business in Today's Environment In a fast-paced market and increasing...
By Leonard Pokrovski 2024-08-03 21:47:02 0 22K

BigMoney.VIP Powered by Hosting Pokrov