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.

Căutare
Categorii
Citeste mai mult
Business
Typical Interview Questions in Management Science: What to Expect and How to Prepare
Interviewing for a role in Management Science—whether in analytics, operations research, or...
By Dacey Rankins 2025-07-05 18:45:54 0 6K
Business
What Happens in the Opening of a Negotiation?
Introduction: The First Moments Shape Everything Every negotiation has a beginning—the...
By Dacey Rankins 2025-11-28 14:40:20 0 2K
Thesauri
Cultural Thesauri
DIALOGUE OF CULTURAL THESAURIES Dialogue of cultures is a certain stadial form of their...
By FWhoop Xelqua 2022-10-01 17:05:50 0 25K
Finance
What Documents Do I Need to Exchange or Buy Foreign Currency?
What Documents Do I Need to Exchange or Buy Foreign Currency? Whether you’re heading...
By Leonard Pokrovski 2025-10-17 08:16:21 0 4K
Business
What are the Steps in the Customer Development Process?
The Customer Development process is a strategic framework used by entrepreneurs, startups, and...
By Dacey Rankins 2025-03-25 15:36:40 0 11K

BigMoney.VIP Powered by Hosting Pokrov