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
Business
Key Challenges and Outcomes: What’s Most Difficult and Impactful?
Mentoring and coaching relationships can be transformative—but they’re not without...
By Dacey Rankins 2025-07-23 12:11:47 0 4K
Business
What Is the Best Sales Technique?
There is no single “best” sales technique for every situation — because...
By Dacey Rankins 2025-12-12 16:58:48 0 2K
Personal Development
How do I improve time management skills?
How Do I Improve Time Management Skills? Time is one of the most valuable resources you have,...
By Michael Pokrovski 2026-02-06 00:43:21 0 3K
Quotations
Quotes from great people
We all patiently count down the last days until spring arrives. But it is better to spend this...
By FWhoop Xelqua 2022-11-13 12:39:27 0 24K
Weight Loss
Weight Loss: Understanding the Journey to a Healthier You
Weight loss is a common goal for many people, whether for improving overall health, enhancing...
By Dacey Rankins 2024-11-21 16:15:13 0 12K

BigMoney.VIP Powered by Hosting Pokrov