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
Television
Medibiz TV. Live TV, Dubai - United Arab Emirates. (English)
Medi BizTV is a 24 hour free to air satellite TV channel dedicated to serve and spread the...
By Nikolai Pokryshkin 2023-03-04 15:05:40 0 57K
Marketing and Advertising
How Far in Advance Should I Submit a Newspaper Ad?
Timing is one of the most overlooked factors in newspaper advertising. Many advertisers focus on...
By Dacey Rankins 2026-01-27 19:43:05 0 976
Human Resources
What Industries Rely Most on Knowledge Capital?
In the modern digital economy, knowledge capital has become a primary driver of value creation...
By Dacey Rankins 2026-03-27 17:58:07 0 2K
Personal Finance
What Is an Emergency Fund, and How Much Should I Have?
What Is an Emergency Fund, and How Much Should I Have? Life is full of surprises—some...
By Leonard Pokrovski 2025-10-21 20:12:28 0 4K
Human Resources
How Do Companies Manage Offshore Teams?
Managing offshore teams has become a core capability in today’s global business...
By Dacey Rankins 2026-03-31 18:03:55 0 5K

BigMoney.VIP Powered by Hosting Pokrov