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
Crafts
Exploring the World of Arts and Crafts: A Creative Journey
Arts and crafts have been a cherished part of human culture for centuries, blending creativity...
Par Dacey Rankins 2024-11-22 14:34:26 0 12KB
Business
What Skills Are Essential for a CEO?
Being a Chief Executive Officer (CEO) requires more than just experience — it demands a...
Par Dacey Rankins 2025-06-13 17:39:19 0 7KB
Marketing and Advertising
What Is the Best Time to Run Radio Ads?
Choosing the right time to run your radio ads is just as important as writing a great script or...
Par Dacey Rankins 2026-02-06 19:34:50 0 2KB
Personal Finance
How Do I Stay Motivated to Save Money?
How Do I Stay Motivated to Save Money? Strategies, Psychological Tricks, and Visual Systems to...
Par Leonard Pokrovski 2025-12-09 16:53:36 0 3KB
Business
Who Needs to Be at This Meeting?
Clarifying Attendees, Roles, and Avoiding Unnecessary Participants In today’s fast-paced...
Par Dacey Rankins 2025-07-31 16:48:04 0 6KB

BigMoney.VIP Powered by Hosting Pokrov