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
E-Commerce
E-commerce
E-commerce is an area of the digital economy that includes all financial and trade transactions...
Par FWhoop Xelqua 2023-07-27 18:12:03 0 36KB
Business
What Equipment is Necessary for Remote Work?
As remote work continues to rise in popularity, ensuring that employees have the proper equipment...
Par Dacey Rankins 2025-01-29 16:58:23 0 14KB
Personal Finance
How to save money from unexpected expenses
What will you learn?   What is a financial safety cushion?   What...
Par FWhoop Xelqua 2022-10-10 12:04:22 0 25KB
Television
Revelation Live TV. United Kingdom.
Our Mission is to use Media to strengthen the Christian voice and impact the nations, with the...
Par Nikolai Pokryshkin 2022-11-28 18:16:17 0 35KB
Personal Finance
How Is My Credit Score Calculated?
How Is My Credit Score Calculated? Your credit score is one of the most important numbers in...
Par Leonard Pokrovski 2025-10-24 11:16:49 0 4KB

BigMoney.VIP Powered by Hosting Pokrov