JavaScript set()

0
7KB

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
Nach Verein filtern
Read More
Science Fiction and Fantasy
Jurassic Park Anthology. (1993-2019) all movies
Jurassic Park (1993) The head of the company "In-Gen", Professor John Hammond, finds a way to...
Von Leonard Pokrovski 2023-02-18 16:59:43 0 20KB
Illustration
The Art of Illustration: A Journey Through Visual Storytelling
Illustration is one of the most powerful forms of visual communication, blending artistry with...
Von Dacey Rankins 2024-11-20 16:36:14 0 5KB
Life Issues
Naked Weapon. (2002)
40 13 y.o. girls are kidnapped and the next 6 years forcefully trained to be sexy assassins. The...
Von Leonard Pokrovski 2023-05-17 20:27:00 0 25KB
Aviation
The fastest aircraft in the world and its competitors - How fast do they fly?
Modern aircraft are able to fly very fast. By "fast" I don't mean speeds 10 times faster than a...
Von FWhoop Xelqua 2022-10-25 11:02:46 0 18KB
Scouting
Exploring the World of Scouting: The Art of Recreation
Exploring the World of Scouting: The Art of Recreation Scouting, with its ethos of outdoor...
Von Leonard Pokrovski 2024-05-17 00:13:46 0 12KB
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov