JavaScript set()

0
11KB

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.

Rechercher
Catégories
Lire la suite
Marketing and Advertising
What Agencies Were Founded by Famous Advertisers?
Some of the world’s most influential advertising agencies did not begin as large corporate...
Par Dacey Rankins 2026-01-08 11:56:18 0 2KB
Music
5 Calming Meditation Music for Deep Relaxation, Focus, and Inner Peace
In today’s fast-paced world, finding a moment of peace can feel like chasing a moving...
Par Gpykin 2025-07-05 17:54:19 0 24KB
Mental Health
ADHD: History
Hyperactivity has long been part of the human condition. Sir Alexander Crichton describes "mental...
Par Kelsey Rodriguez 2023-05-08 19:41:00 0 13KB
Business
Am I Prepared to Handle the Challenges of Running a Business?
Starting and running a business is an exciting and rewarding endeavor, but it’s also one of...
Par Dacey Rankins 2025-02-07 16:17:33 0 20KB
Business
What Is Bounce Rate?
In digital marketing and website optimization, one of the most frequently discussed metrics is...
Par Dacey Rankins 2025-08-28 17:43:39 0 7KB

BigMoney.VIP Powered by Hosting Pokrov