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.

Search
Categories
Read More
Business
Mentoring Frequently Asked Questions (FAQs)
Mentorship is a powerful tool for personal and professional growth. Whether you're new to...
By Dacey Rankins 2025-05-27 14:16:25 0 7K
Horror
Psycho. (1960)
A Phoenix secretary embezzles $40,000 from her employer's client, goes on the run and checks into...
By Leonard Pokrovski 2022-11-25 18:20:00 0 24K
Marketing and Advertising
The Importance and Benefits of YouTube Marketing — A Strategic Overview
Introduction YouTube has evolved from a simple video-sharing website into one of the most...
By Dacey Rankins 2025-11-13 17:15:48 0 1K
Analysis and Opinion
How to Use Critical Thinking in the Information Flow
What is Critical ThinkingCritical thinking is the ability to doubt incoming information and one's...
By FWhoop Xelqua 2022-09-25 10:47:13 0 29K
Programming
JavaScript Map
Map objects are collections of key-value pairs. A key in the Map may only...
By Jesse Thomas 2023-05-19 20:41:47 0 11K

BigMoney.VIP Powered by Hosting Pokrov