JavaScript set()

0
8K

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
Bibliography
Unlocking the Power of References in Bibliographies: A Comprehensive Guide
Unlocking the Power of References in Bibliographies: A Comprehensive Guide Bibliographies are...
By Leonard Pokrovski 2024-05-16 23:57:31 0 13K
Business
How to Get Yourself 'Unstuck' at Work: Practical Strategies to Regain Momentum
Feeling stuck at work can be frustrating, demotivating, and even a little scary—especially...
By Dacey Rankins 2025-05-22 20:10:30 0 5K
Телевидение
Прямой эфир телеканала Пятница
Телеканал Пятница для тех, кто охотится за впечатлениями и хочет попробовать мир на вкус....
By Nikolai Pokryshkin 2022-10-04 14:16:15 1 24K
Business
How Do CEOs Foster a Positive Organizational Culture?
A company’s culture—the shared values, beliefs, and behaviors that shape how work...
By Dacey Rankins 2025-06-16 15:01:20 0 2K
Life Issues
Top Gun: Maverick. (2022)
After thirty years, Maverick is still pushing the envelope as a top naval aviator, but must...
By Leonard Pokrovski 2023-01-15 18:16:42 0 17K

BigMoney.VIP Powered by Hosting Pokrov