JavaScript set()
Δημοσιευμένα 2023-05-17 19:24:16
0
8χλμ.
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
.
Αναζήτηση
Κατηγορίες
- Arts
- Business
- Computers
- Παιχνίδια
- Health
- Κεντρική Σελίδα
- Kids and Teens
- Money
- News
- Recreation
- Reference
- Regional
- Science
- Shopping
- Society
- Sports
- Бизнес
- Деньги
- Дом
- Досуг
- Здоровье
- Игры
- Искусство
- Источники информации
- Компьютеры
- Наука
- Новости и СМИ
- Общество
- Покупки
- Спорт
- Страны и регионы
- World
Διαβάζω περισσότερα
BUSINESS MANAGEMENT: 12 STEPS TO EFFICIENCY
BUSINESS MANAGEMENT: 12 STEPS TO EFFICIENCY
In modern business, the key element of success...
The Power of Activism: Driving Change in Society
Activism is the practice of taking action to bring about social or political change. It...
Shock, awe and suicide. How the Genetic Program Drives People to the Grave
The biggest threat to humans is biological aging, that is, the slow decline of bodily functions,...
Price elasticity of demand and price elasticity of supply
Key points
Price elasticity measures the responsiveness of the quantity...
Barry Lyndon. (1975)
An Irish rogue wins the heart of a rich widow and assumes her dead husband's aristocratic...