JavaScript set()

0
8K

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
Cybernetics
Cybernetics Cybernetics (from Ancient...
By Leonard Pokrovski 2024-08-21 01:08:17 0 17K
Business
What are the Tax Implications of Working Remotely?
As the global workforce increasingly shifts to remote work, understanding the tax implications of...
By Dacey Rankins 2025-01-30 15:45:16 0 10K
Real Estate
The State of Real Estate in 2024: Trends, Challenges, and Opportunities
The real estate market is often viewed as a barometer for the broader economy, reflecting both...
By Dacey Rankins 2024-11-06 17:29:41 0 7K
Nature and Science
A Beautiful Mind. (2001)
After John Nash, a brilliant but asocial mathematician, accepts secret work in cryptography, his...
By Leonard Pokrovski 2023-02-17 16:55:42 0 30K
Play-By-Mail
The World of Play-by-Mail Games: A Nostalgic and Strategic Gaming Experience
In today’s digital age, video games and online multiplayer experiences dominate the gaming...
By Dacey Rankins 2024-11-22 14:37:53 0 9K
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov