JavaScript set()

0
12K

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
Can a business biography help in networking?
In today’s interconnected world, networking is a key aspect of building a successful career...
By Dacey Rankins 2025-01-07 14:18:30 0 27K
Financial Services
How changes in the cost of living are measured
Key points The Consumer Price Index, or CPI is a measure of...
By Mark Lorenzo 2023-06-09 20:21:34 0 15K
Business
What Should I Think About as I Prepare to Meet with a Mentor?
Meeting with a mentor can be a pivotal step in your personal and professional growth. Whether...
By Dacey Rankins 2025-05-26 16:51:01 0 5K
Marketing and Advertising
YouTube Marketing – Key Questions, Strategies & Best Practices
Introduction YouTube is no longer just a video-sharing platform—it’s a search...
By Dacey Rankins 2025-11-14 20:51:09 0 7K
Business
Marketplace vs Dropshipping: Two Popular Business Models That Are Far Less Similar Than They Appear
At a distance, they look remarkably alike. Neither requires owning a warehouse. Neither demands...
By Dacey Rankins 2026-06-17 14:20:11 0 1K

BigMoney.VIP Powered by Hosting Pokrov