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
Do I Need a Business Plan?
If you’re thinking about starting a business, you might wonder whether a business plan is...
By Dacey Rankins 2025-03-13 15:44:34 0 18K
Business
What Do You Like Most About Working for This Company?
During a job interview, one insightful question candidates often ask is: "What do you like most...
By Dacey Rankins 2025-06-24 15:32:48 0 6K
Marketing and Advertising
Should I Do Market Research Myself or Hire an Agency? (Complete 2025 Guide)
Introduction: The Dilemma Every Business Faces Market research is one of the most crucial...
By Dacey Rankins 2025-10-09 18:42:33 0 2K
Marketing and Advertising
Why Is Community Building Important?
In today’s digital-first and interconnected world, community building has become one of the...
By Dacey Rankins 2025-09-26 15:06:09 0 3K
Gay, Lesbian, and Bisexual
LGBTQIA+
LGBT is an acronym for lesbian, gay, bisexual, and transgender people. It appeared in English in...
By FWhoop Xelqua 2023-03-22 13:29:38 0 19K

BigMoney.VIP Powered by Hosting Pokrov