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
Open-Ended Feedback: Unlocking Deeper Insights from Your Customers
When it comes to truly understanding customer sentiment, open-ended feedback can be more...
Par Dacey Rankins 2025-07-25 14:09:58 0 7KB
Business and Corporate Finance
What Skills Does a CFO Need?
What Skills Does a CFO Need? Financial, Strategic, Leadership, and Communication Skills The...
Par Leonard Pokrovski 2026-01-12 18:55:44 0 2KB
Points activés
Navigating the Complexities of Disability in Modern Society
Navigating the Complexities of Disability in Modern Society Introduction:In the mosaic of human...
Par Leonard Pokrovski 2024-06-06 18:38:08 0 25KB
Programming
Python Motor vs Pymongo
Motor provides a single client class, MotorClient. Unlike PyMongo’s MongoClient,...
Par Jesse Thomas 2023-07-12 21:04:17 0 13KB
Business and Corporate Finance
How Does Corporate Finance Manage Risk?
How Does Corporate Finance Manage Risk? Hedging, Diversification, and Financial Controls...
Par Leonard Pokrovski 2026-01-21 19:26:25 0 3KB

BigMoney.VIP Powered by Hosting Pokrov