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
Business and Corporate Finance
What Is Financial Leverage in Corporate Finance?
What Is Financial Leverage in Corporate Finance? Understanding Its Relationship with Risk and...
By Leonard Pokrovski 2026-01-22 20:09:03 0 5K
Business
Can a License Be Revoked?
Most people think about licenses as permissions. Permission to use software. Permission to sell...
By Dacey Rankins 2026-06-13 12:36:00 0 2K
Life Issues
Before Sunrise. (1995)
A young man and woman meet on a train in Europe, and wind up spending one evening together in...
By Leonard Pokrovski 2023-03-20 20:33:26 0 27K
Marketing and Advertising
Is PR the Same as Marketing or Advertising? Understanding the Key Differences
Introduction: The Confusion Between PR, Marketing, and Advertising Public relations (PR),...
By Dacey Rankins 2025-10-24 18:26:00 0 5K
Productivity
How to avoid forgetting new skills?
How to Avoid Forgetting New Skills? Learning a new skill feels exciting. Progress is...
By Michael Pokrovski 2026-05-29 22:29:39 0 2K

BigMoney.VIP Powered by Hosting Pokrov