JavaScript set()

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.

Поиск
Категории
Больше
Biotechnology and Pharmaceuticals
Biotechnology: Unlocking the Future of Medicine, Agriculture, and Sustainability
Biotechnology, a field at the intersection of biology and technology, has revolutionized how we...
От Dacey Rankins 2024-11-12 14:34:17 0 8Кб
Business
How Does Indie Hacking Differ from Traditional Entrepreneurship?
In the world of startups and business ventures, the terms indie hacking and traditional...
От Dacey Rankins 2025-02-14 15:34:17 0 9Кб
Business
How Do Startup CEOs Handle Decision-Making and Leadership?
In the high-stakes world of startups, where uncertainty is the norm and resources are limited,...
От Dacey Rankins 2025-05-02 15:01:19 0 4Кб
Business
What Industries Do Business Consultants Typically Work In?
Business consultants play a crucial role in helping companies solve problems, optimize...
От Dacey Rankins 2025-02-11 15:50:27 0 7Кб
Научная фантастика и фэнтези
Битва у Биг-Рок. Battle at Big Rock. (2019)
В самом предверии выхода фильма “Мир юрского периода: Господство” в кинотеатры свет...
От Nikolai Pokryshkin 2023-02-18 11:27:03 0 25Кб

BigMoney.VIP Powered by Hosting Pokrov