JavaScript set()

0
11KB

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.

Suche
Kategorien
Mehr lesen
Business
How to Write a Business Plan for a Small Business: A Step-by-Step Guide
Writing a business plan is an essential step for any small business owner looking to formalize...
Von Dacey Rankins 2024-12-23 15:59:47 0 12KB
Business
Top 15 CEO Interview Questions: What to Ask Your Next Leader
Hiring a Chief Executive Officer (CEO) is one of the most critical decisions any company can...
Von Dacey Rankins 2025-06-17 16:01:22 0 10KB
Business
Text of the scientific work on the topic "ANALYSIS OF MODERN APPROACHES TO THE DEFINITION OF INNOVATIVE ACTIVITY OF THE ENTERPRISE"
Text of the scientific work on the topic "ANALYSIS OF MODERN APPROACHES TO THE DEFINITION OF...
Von Leonard Pokrovski 2024-08-22 12:49:36 0 20KB
Medien
The Call (2020)
In the fall of 1987, a group of small-town friends must survive the night in the home of a...
Von Leonard Pokrovski 2022-09-08 18:40:18 1 33KB
Business
How Does Mentoring Differ from Coaching?
Mentoring and coaching are both powerful tools for personal and professional development, but...
Von Dacey Rankins 2025-07-07 14:33:50 0 5KB

BigMoney.VIP Powered by Hosting Pokrov