JavaScript set()

0
11K

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.

Cerca
Categorie
Leggi tutto
Business
What Platforms Can I Use for Crowdfunding?
Crowdfunding has become a popular way for individuals, businesses, and organizations to raise...
By Dacey Rankins 2025-03-27 18:10:48 0 8K
Business
What Is a Project Management Plan?
A Project Management Plan is a comprehensive, formal document that outlines how a project will be...
By Dacey Rankins 2025-05-15 14:34:45 0 7K
Business
How Do Startup Founders Build a Strong Company Culture?
Building a strong company culture is one of the most important and often challenging tasks for...
By Dacey Rankins 2025-04-03 14:59:41 0 10K
Marketing and Advertising
What Are the 4 Ps of Marketing? A Complete Guide to the Marketing Mix
Introduction: Why Every Marketer Needs to Master the 4 Ps Whether you’re running a...
By Dacey Rankins 2025-10-13 18:02:28 0 2K
Business
How to Become a CEO: A Step-by-Step Guide
Becoming a CEO is the pinnacle of success for many professionals in the business world....
By Dacey Rankins 2025-06-17 15:59:24 0 9K

BigMoney.VIP Powered by Hosting Pokrov