JavaScript set()

0
6KB

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.

Pesquisar
Categorias
Leia mais
Products and Shopping
10 health products that will not hit the wallet
Hello, dear readers! Today we will talk about useful products for health. As you...
Por FWhoop Xelqua 2022-09-15 18:09:33 0 43KB
Climbing
Professional Rock Climbing Tournaments: A Thrilling Fusion of Skill, Strategy, and Strength
  Professional Rock Climbing Tournaments: A...
Por Dacey Rankins 2024-12-06 15:32:53 0 4KB
Emulators
9 Best Android Emulators for PC and Mac
Why do I need an emulator? Android emulators allow you to run smartphone apps and games on your...
Por FWhoop Xelqua 2022-10-03 16:50:33 0 25KB
Television
SMNI News (English) Live TV Philippines
Sonshine Media Network International (SMNI), is the official broadcasting arm of the Kingdom of...
Por Nikolai Pokryshkin 2022-08-30 21:56:36 0 24KB
Business
What are the Tax Implications of Working Remotely?
As the global workforce increasingly shifts to remote work, understanding the tax implications of...
Por Dacey Rankins 2025-01-30 15:45:16 0 4KB
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov