JavaScript set()

0
10K

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
Social Issues
No Country for Old Men. (2007)
Violence and mayhem ensue after a hunter stumbles upon a drug deal gone wrong and more than two...
By Leonard Pokrovski 2023-02-23 18:42:03 0 19K
Business
How Will We Track Progress and Report Performance? Using Gantt, Kanban, Charts, KPIs, Dashboards, and Status Reports
Tracking project progress and reporting performance are essential for keeping teams aligned,...
By Dacey Rankins 2025-07-14 14:45:38 0 3K
Financial Services
The benefits and costs of US environmental laws
Key points Taken as a whole, the benefits of US environmental regulation have...
By Mark Lorenzo 2023-02-20 17:47:53 0 10K
Business
What is the CEO's Educational Background?
Understanding a CEO’s educational background provides valuable insight into the knowledge...
By Dacey Rankins 2025-06-30 14:02:04 0 4K
Телевидение
6ТВ прямая трансляция, г. Горловка.
Телеканал «6ТВ» г. Горловка - первооткрыватель ТВ-пространства в Горловке. За годы...
By Nikolai Pokryshkin 2022-11-14 19:10:38 0 36K

BigMoney.VIP Powered by Hosting Pokrov