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
Life Issues
Addicted. (2014)
A gallerist risks her family and flourishing career when she enters into an affair with a...
By Leonard Pokrovski 2023-05-21 18:12:36 0 34K
Business
What Are the Key Types of Business Strategies?
Business strategy is an essential part of any organization’s long-term success. It involves...
By Dacey Rankins 2024-12-23 16:04:34 0 21K
Business
5 reasons to work if you already have enough money
Many work primarily for money.But this is not everyone's only motivation. Many people advise...
By Dacey Rankins 2024-09-24 13:46:25 0 17K
Business
Who Initiates the Mentoring? Mentor or Mentee?
One of the most common questions about mentoring is: “Who should make the first...
By Dacey Rankins 2025-07-22 11:06:10 0 3K
Business
How Would You Describe the Company Culture?
Understanding the Work Environment, Communication Style, and Overall Atmosphere When job seekers...
By Dacey Rankins 2025-06-10 15:25:12 0 5K

BigMoney.VIP Powered by Hosting Pokrov