JavaScript set()

0
9K

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.

Search
Categories
Read More
Television
ABC Action News. WFTS Tampa Bay, Florida, USA.
WFTS-TV is an ABC-affiliated television station located in Tampa, Florida, United States. It is...
By Nikolai Pokryshkin 2022-10-29 12:06:35 0 44K
Business
15 Interview Questions About Company Culture (With Advice)
When you're interviewing for a new job, it's easy to focus on salary, job duties, and growth...
By Dacey Rankins 2025-06-13 17:35:00 0 7K
Financial Services
Your 401(k): 10 things to find out
1. When can I start contributing? Not every 401(k) plan allows new employees to begin...
By Mark Lorenzo 2023-05-25 18:45:54 0 10K
Living History
Living History Recreation: Bringing the Past to Life Through Timeless Experience
Living History Recreation: Bringing the Past to Life Through Timeless Experience In the vast...
By Leonard Pokrovski 2024-05-14 23:29:07 0 17K
Business
Crowdfunding for Business: What It Is, How It Works, and What Are the Crowdfunding Platforms for Projects
Crowdfunding for Business: What It Is, How It Works, and What Are the Crowdfunding Platforms for...
By Leonard Pokrovski 2024-08-01 21:59:41 0 17K

BigMoney.VIP Powered by Hosting Pokrov