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 Do I Pitch to the Media?
Getting media coverage can be a game-changer for startups, entrepreneurs, authors, and small...
Par Dacey Rankins 2025-12-03 15:51:10 0 1KB
Marketing and Advertising
How Is PR Measured? What KPIs Matter Most in Public Relations
Introduction: The Shift Toward Data-Driven PR Public relations used to be considered more art...
Par Dacey Rankins 2025-10-27 17:06:34 0 2KB
Business
What Are the Best Small Business Loans or Grants Available?
Starting or expanding a small business often requires financial support. Fortunately, various...
Par Dacey Rankins 2025-03-14 16:10:16 0 13KB
Bulletin Board Systems
Electronic Bulletin Boards
An electronic bulletin board is a website where ads are placed. Initially, this concept referred...
Par FWhoop Xelqua 2023-01-27 18:21:49 0 22KB
Business
What Could Go Wrong? Identifying Risk Factors and Planning Mitigations
Every project, regardless of size or complexity, carries an element of uncertainty. Identifying...
Par Dacey Rankins 2025-07-11 19:57:06 0 6KB

BigMoney.VIP Powered by Hosting Pokrov