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 Did They Handle Failure or Setbacks?
No leader rises to prominence without encountering obstacles. In fact, the way a leader handles...
Par Dacey Rankins 2025-06-27 16:39:25 0 5KB
Business
Drew Houston
Drew Houston Drew Houston Andrew W. "Drew" Houston   Date of...
Par Leonard Pokrovski 2024-08-16 14:54:55 0 24KB
Personal Finance
What Is a Good Salary?
What Is a Good Salary? Evaluating $70k, $50k, and Income Across Different Cities and Situations...
Par Leonard Pokrovski 2025-11-27 18:53:29 0 2KB
Marketing and Advertising
The Unique Benefits of Direct Mail Marketing
Introduction: Why Direct Mail Still Matters In the digital-first age, marketers often assume...
Par Dacey Rankins 2025-10-02 20:04:36 0 4KB
Small Business
How to Grow Your Small Business: Key Strategies for Success
Running a small business is a remarkable achievement, but growing it into a larger, sustainable...
Par Dacey Rankins 2024-11-13 16:32:44 0 14KB

BigMoney.VIP Powered by Hosting Pokrov