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.

Site içinde arama yapın
Kategoriler
Read More
Business
How Do You Manage Sales Performance?
Managing sales performance is not about watching numbers and reacting when things go wrong....
By Dacey Rankins 2025-12-19 19:12:43 0 3K
Consultants
20 ways to attract visitors to the site through your personal link!
How to attract visitors to the site through your personal link? This question is asked by almost...
By Nikolai Pokryshkin 2022-08-14 22:31:09 0 45K
Politics
Politics
Politics, the dynamic realm where power, policy, and people intersect, remains one of the most...
By Leonard Pokrovski 2024-06-11 21:38:51 0 19K
Television
FOX 13 Live TV Seattle USA
Seattle & Western Washington news, weather, traffic, politics & sports. FOX 13 Seattle...
By Nikolai Pokryshkin 2022-09-10 08:45:39 0 47K
Marketing and Advertising
What Does PPC Stand For?
PPC is one of the most commonly used terms in digital marketing, yet many people encounter it...
By Dacey Rankins 2026-02-03 19:07:58 0 2K

BigMoney.VIP Powered by Hosting Pokrov