JavaScript set()

0
11K

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.

Buscar
Categorías
Read More
Television
Tulalip Live TV. USA.
TULALIP TV provides programming to Tulalip Tribal members and households around Indian country....
By Nikolai Pokryshkin 2022-10-28 10:37:33 0 29K
Religion and spirituality
Exploring the Interplay Between Religion and Spirituality
Exploring the Interplay Between Religion and Spirituality In the realm of human experience, few...
By Leonard Pokrovski 2024-06-14 00:34:45 0 23K
Algorithms
Understanding Computer Algorithms: The Heart of Modern Computing
In the world of computer science, algorithms are fundamental building blocks that drive...
By Dacey Rankins 2024-11-19 18:10:39 0 11K
Programming
If and Else statements or Try and Except Statements
If and Else statements should be used if you want to check if something is True or false. This...
By Jesse Thomas 2023-02-22 21:19:36 0 11K
Business
How Does Feedback (e.g., Win/Loss Data) Influence Your Roadmap?
When building and marketing products, success depends on more than vision—it requires...
By Dacey Rankins 2025-09-16 16:18:18 0 5K

BigMoney.VIP Powered by Hosting Pokrov