JavaScript set()

0
11KB

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.

Rechercher
Catégories
Lire la suite
Business
What Is Pipeline Management?
Pipeline management is the engine room of sales management. You can have great salespeople,...
Par Dacey Rankins 2025-12-23 19:43:00 0 2KB
Жизненные вопросы
Клаус. Klaus. (2019)
Владелец почтовой империи, чтобы научить ленивого отпрыска по имени Джеспер уму-разуму,...
Par Nikolai Pokryshkin 2023-03-18 11:47:08 0 34KB
Business
What Does It Mean to Pivot in Lean Startup Terms?
In the world of startups, the ability to adapt and change direction is crucial for survival. One...
Par Dacey Rankins 2025-03-10 17:17:09 0 11KB
Social Issues
Hillbilly Elegy (2020)
Based on the bestselling memoir by J.D. Vance, HILLBILLY ELEGY is a modern exploration of the...
Par Leonard Pokrovski 2022-10-09 18:30:12 0 27KB
Business
What Is the Sales Funnel?
The sales funnel is one of the most important concepts in sales and marketing. Almost every sales...
Par Dacey Rankins 2025-12-16 20:13:38 0 1KB

BigMoney.VIP Powered by Hosting Pokrov