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.

Pesquisar
Categorias
Leia Mais
Financial Services
Decisions with a budged constraint
Key points The budget constraint is the boundary of...
Por Mark Lorenzo 2023-02-13 15:15:43 0 15K
Business
What Types of Startup Incubators Exist?
Startup incubators have become essential pillars in the global entrepreneurship ecosystem,...
Por Dacey Rankins 2025-04-24 16:56:55 0 13K
Entertainment
The World of Entertainment Arts: An Overview of Its Impact and Diversity
The realm of entertainment arts encompasses a broad spectrum of creative disciplines designed to...
Por Dacey Rankins 2024-11-26 15:00:39 0 12K
Internet Broadcasts
The Rise of Internet Broadcast News: Redefining Media Consumption
The Rise of Internet Broadcast News: Redefining Media Consumption In an era characterized by...
Por Leonard Pokrovski 2024-05-03 19:30:15 0 17K
Business
What Is Growth Hacking?
In today’s fast-paced digital landscape, businesses are under constant pressure to grow...
Por Dacey Rankins 2025-09-11 16:25:46 0 3K

BigMoney.VIP Powered by Hosting Pokrov