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
Programming
asyncio.sleep() vs time.sleep()
Both of these functions basically has the same purpose, but a little different. Time.sleep()...
By Jesse Thomas 2023-03-15 19:00:59 0 13K
Contests
International Art Exhibit
All authors working in the formats of contemporary art are invited to participate in the NordArt...
By FWhoop Xelqua 2023-02-26 17:44:53 0 19K
Business
What Are Common Barriers to Innovation, and How Can They Be Overcome?
Innovation is often hailed as the key to progress and growth in any organization or industry....
By Dacey Rankins 2025-03-20 14:58:44 0 10K
Financial Services
Types of market-oriented environmental tools
Key points The three main categories of market-oriented environmental policies are...
By Mark Lorenzo 2023-02-07 17:15:56 0 12K
EĞİTİM BİLGİLERİ
Health Education: Empowering Individuals and Communities for Better Health
Health education is a vital component of public health that aims to empower individuals and...
By Dacey Rankins 2024-11-21 16:04:40 0 12K

BigMoney.VIP Powered by Hosting Pokrov