JavaScript set()

0
11K

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.

Cerca
Categorie
Leggi tutto
Жизненные вопросы
Поймать последнюю волну. Caught by a Wave. (2021)
Невероятно драматическая история любви, которая в свою очередь поведает о двух подростках,...
By Nikolai Pokryshkin 2022-09-17 09:21:53 0 33K
Pre-School
The Importance of Preschool: Laying the Foundation for Lifelong Learning
Preschool is often seen as the first formal experience for young children before they enter...
By Dacey Rankins 2024-11-27 17:49:03 0 13K
Personal Finance
How Long Does It Take to Pay Off Student Loans?
How Long Does It Take to Pay Off Student Loans? How Repayment Plan, Loan Amount, and Income...
By Leonard Pokrovski 2025-12-19 23:08:51 0 2K
Business
How to Give Feedback Effectively – Frameworks, Real-World Examples, and Best Practices
Giving feedback is a powerful skill—whether you're a manager, colleague, teacher, or peer....
By Dacey Rankins 2025-07-30 11:32:58 0 6K
Business
How Can I Get My First Customers?
Getting your first customers is one of the biggest challenges when starting a business. Without a...
By Dacey Rankins 2025-03-14 16:16:11 0 13K

BigMoney.VIP Powered by Hosting Pokrov