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
The Best Public Speaking Tips: Techniques and Habits for Confident Delivery
Public speaking can be intimidating, but mastering it opens doors in education, business,...
Par Dacey Rankins 2025-12-10 22:24:51 0 2KB
Dice
Dice
Dice are a popular source of randomness in board games (especially the game of the same name)....
Par Dacey Rankins 2024-04-03 16:00:58 0 15KB
Телевидение
Прямой эфир телеканала Енисей.
Краевой телеканал «Енисей»  — это развивающаяся и динамичная компания,...
Par Nikolai Pokryshkin 2022-10-30 20:38:41 0 27KB
Social Issues
Citizenfour (2014)
A documentarian and a reporter travel to Hong Kong for the first of many meetings with Edward...
Par Leonard Pokrovski 2023-07-22 17:12:43 0 26KB
Business
How to Analyze Open-Ended Feedback
Open-ended feedback is a powerful source of insights that helps businesses, educators,...
Par Dacey Rankins 2025-07-30 11:23:00 0 7KB

BigMoney.VIP Powered by Hosting Pokrov