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
Personal Finance
What Is the Median Salary in My City or Country?
What Is the Median Salary in My City or Country? A Practical Guide to Understanding and Finding...
Par Leonard Pokrovski 2025-11-28 21:27:54 0 3KB
E-Books
The Rise and Impact of E-books: Revolutionizing Reading in the Digital Age
In the digital age, e-books have transformed the way people read and interact with literature....
Par Dacey Rankins 2024-11-20 16:29:33 0 10KB
Business
Who Can Benefit from Mentoring?
Mentoring is often associated with early-career professionals, but its value extends far beyond...
Par Dacey Rankins 2025-07-18 17:35:49 0 4KB
Business
What Is Viral Growth?
In the digital age, many of the fastest-growing companies—Facebook, TikTok, Dropbox, and...
Par Dacey Rankins 2025-09-17 10:44:08 0 4KB
Television
FOX SOUL Live TV USA
Celebrate Black Culture. FOX SOUL is a streaming platform for The Culture! We amplify Black...
Par Nikolai Pokryshkin 2022-09-04 08:47:33 0 40KB

BigMoney.VIP Powered by Hosting Pokrov