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.

Search
Categories
Read More
Television
FOX 29, Live TV. Philadelphia USA
Fox 29 — which may also be called Fox News 29, or WTXF — has a great live stream....
By Nikolai Pokryshkin 2022-09-18 09:09:19 0 45K
Business
What Is a Business Biography?
A business biography is a detailed account of a person’s career and professional...
By Dacey Rankins 2024-12-27 15:56:10 0 17K
Television
Our Journey with God. Live TV.
Our Journey with God. Jesus Live network. Channel 3. My Link
By Nikolai Pokryshkin 2022-10-06 08:55:48 0 30K
Programming
Make A 2D Game With Unity
How to Create a 2D Game in Unity To create 2D games, we need to use Unity Hub. Unity...
By Jesse Thomas 2023-06-28 21:18:55 0 13K
Economics
How Does Capitalism Work?
How Does Capitalism Work?Markets, Ownership, and Incentives Capitalism is one of the most common...
By Leonard Pokrovski 2026-02-06 23:18:06 0 1K

BigMoney.VIP Powered by Hosting Pokrov