JavaScript set()

0
11Кб

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.

Поиск
Категории
Больше
Business
How AI is Impacting Entrepreneurship
In recent years, Artificial Intelligence (AI) has emerged as a game-changing technology,...
От Dacey Rankins 2025-03-19 14:55:25 0 15Кб
Television
FOX Weather Live TV USA
Streaming 24/7 from America's Weather Center.Fox Weather is a digital broadcast television...
От Nikolai Pokryshkin 2022-09-05 08:52:17 0 34Кб
Tobacco
Navigating the World of Tobacco: A Comprehensive Guide to Informed Shopping
Navigating the World of Tobacco: A Comprehensive Guide to Informed Shopping Introduction: In the...
От Leonard Pokrovski 2024-06-05 00:55:27 0 23Кб
Business
How Do I Overcome Fear of Public Speaking?
Public speaking is one of the most common fears in the world — and it affects people of all...
От Dacey Rankins 2025-12-10 17:38:22 0 3Кб
Mental Health
Psychosis: Disability
DisabilityThe classification of psychosis as a social disability is a common occurrence....
От Kelsey Rodriguez 2023-05-23 16:08:57 0 12Кб

BigMoney.VIP Powered by Hosting Pokrov