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.

Site içinde arama yapın
Kategoriler
Read More
Internet
What is Native Advertising: Examples, Types, Pros and Cons
Native advertising is a form of advertising that looks like a natural context and integrates into...
By FWhoop Xelqua 2023-06-08 17:52:18 0 20K
Business
How Can I Validate My Startup Idea Before Launching?
One of the most critical steps in launching a successful startup is ensuring that your idea has a...
By Dacey Rankins 2025-03-24 15:27:36 0 14K
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...
By Leonard Pokrovski 2024-08-22 12:49:36 0 19K
Business
What Are the Benefits of Mentoring for the Mentee?
Mentoring is a powerful tool for personal and professional growth. While the mentor provides...
By Dacey Rankins 2025-05-24 15:44:21 0 7K
Marketing and Advertising
How Is PR Measured? What KPIs Matter Most in Public Relations
Introduction: The Shift Toward Data-Driven PR Public relations used to be considered more art...
By Dacey Rankins 2025-10-27 17:06:34 0 758

BigMoney.VIP Powered by Hosting Pokrov