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
Television
WGN Live TV. Chicago, Illinois, USA.
WGN-TV, is a CW-affiliated television station locatedin Chicago, Illinois. Broadcasts started in...
By Nikolai Pokryshkin 2022-10-31 12:25:16 0 28K
Programming
Python Booleans
Booleans are expressions that evaluates if it's true or false. For example, if you compare 2...
By Jesse Thomas 2023-02-24 21:41:56 0 12K
Business
How Does Growth Hacking Relate to Growth Marketing?
Over the last decade, the terms growth hacking and growth marketing have become common in...
By Dacey Rankins 2025-09-12 16:26:10 0 5K
Business
What Is a Lead Magnet and How Do I Create One?
A lead magnet is one of the most effective tools in modern lead generation. When done correctly,...
By Dacey Rankins 2025-12-17 18:54:48 0 2K
Business and Corporate Finance
What Is Working Capital?
What Is Working Capital? Working capital is one of the most important concepts in business...
By Leonard Pokrovski 2026-01-07 07:13:37 0 2K

BigMoney.VIP Powered by Hosting Pokrov