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
Business
How Does the Company Support Employee Growth and Development?
In today’s dynamic and competitive job market, organizations that prioritize employee...
Par Dacey Rankins 2025-06-11 13:23:23 0 9KB
Business
What to do if you have a mobile app idea: a beginner's guide.
What to do if you have a mobile app idea: a beginner's guide. One fine morning, you wake up with...
Par Leonard Pokrovski 2024-08-28 22:25:54 0 22KB
Mental Health
Dyslexia: Society and culture
As is the case with any disorder, society often makes an assessment based on incomplete...
Par Kelsey Rodriguez 2023-07-10 17:29:57 0 13KB
Marketing and Advertising
What Industries Spend the Most on Advertising?
Advertising drives awareness, engagement, and revenue, but not all industries invest in it...
Par Dacey Rankins 2026-01-06 15:32:58 0 828
Business
How Do Business Consultants Identify the Needs of a Company?
Business consultants play a crucial role in helping companies identify their needs, challenges,...
Par Dacey Rankins 2025-02-10 18:23:41 0 15KB

BigMoney.VIP Powered by Hosting Pokrov