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.

Search
Categories
Read More
Life Issues
Inside Out. (2016)
After young Riley is uprooted from her Midwest life and moved to San Francisco, her emotions -...
By Leonard Pokrovski 2023-03-06 18:28:32 0 23K
Marketing and Advertising
What Is Cross-Device Remarketing? A Complete Guide to Reaching the Same User on Different Devices
Today’s customers rarely use just one device. They browse on smartphones, compare prices...
By Dacey Rankins 2026-02-12 20:40:02 0 5K
Social Issues
Oldboy. (2003)
After being kidnapped and imprisoned for fifteen years, Oh Dae-Su is released, only to find that...
By Leonard Pokrovski 2022-12-28 20:47:59 0 28K
Business
What Industries Are Next? The Future of the On-Demand Economy
Every transformative business model begins with a question that seems surprisingly ordinary....
By Dacey Rankins 2026-07-02 23:42:14 0 919
Economics
How Do Economic Conditions Impact Consumers?
How Do Economic Conditions Impact Consumers? Economic conditions shape nearly every aspect of...
By Leonard Pokrovski 2026-04-01 07:50:13 0 8K

BigMoney.VIP Powered by Hosting Pokrov