JavaScript set()
Posted 2023-05-17 19:24:16
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
- Arts
- Business
- Computers
- Spiele
- Health
- Home
- Kids and Teens
- Money
- News
- Recreation
- Reference
- Regional
- Science
- Shopping
- Society
- Sports
- Бизнес
- Деньги
- Дом
- Досуг
- Здоровье
- Игры
- Искусство
- Источники информации
- Компьютеры
- Наука
- Новости и СМИ
- Общество
- Покупки
- Спорт
- Страны и регионы
- World
Read More
Jurassic Park Anthology. (1993-2019) all movies
Jurassic Park (1993)
The head of the company "In-Gen", Professor John Hammond, finds a way to...
The Art of Illustration: A Journey Through Visual Storytelling
Illustration is one of the most powerful forms of visual communication, blending artistry with...
Naked Weapon. (2002)
40 13 y.o. girls are kidnapped and the next 6 years forcefully trained to be sexy assassins. The...
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...
Exploring the World of Scouting: The Art of Recreation
Exploring the World of Scouting: The Art of Recreation
Scouting, with its ethos of outdoor...