JavaScript set()
Сообщение 2023-05-17 19:24:16
0
12Кб
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
- Деньги
- News
- Personal Development
- Recreation
- Regional
- Reference
- Science
- Shopping
- Society
- Sports
- Бизнес
- Деньги
- Дом
- Досуг
- Здоровье
- Игры
- Искусство
- Источники информации
- Компьютеры
- Личное развитие
- Наука
- Новости и СМИ
- Общество
- Покупки
- Спорт
- Страны и регионы
- World
Больше
How can I generate passive income?
In today’s world, the pursuit of financial freedom has led many individuals to explore...
How Can I Validate My Business Idea Without Significant Funding?
Validating a business idea is an essential step in the entrepreneurial process, but many aspiring...
What are personal development quotes?
What Are Personal Development Quotes?
Personal development quotes are short, powerful statements...
How do I manage my time better?
How Do I Manage My Time Better?
Time is one of the most valuable resources we have, yet it often...
What Are the Different Types of Influencers?
In the world of social media and digital marketing, influencers play a crucial role in shaping...