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
Personal Finance
How to Create a Budget and Save Money: A Practical Guide to Managing Income, Expenses, and Spending
How to Create a Budget and Save Money: A Practical Guide to Managing Income, Expenses, and...
By Leonard Pokrovski 2025-11-18 21:12:47 0 4K
Economics
Has Brexit Harmed the UK?
Has Brexit Harmed the UK? On January 31, 2020, the United Kingdom formally left the European...
By Leonard Pokrovski 2026-02-05 23:58:01 0 778
Business
How Do I Determine If I Am Capable of Starting a Business?
Starting a business is a bold and exciting venture, but it’s not without its challenges....
By Dacey Rankins 2025-02-28 16:10:37 0 12K
Software
Software
Software (also known as software) is a program or set of programs used to control a computer...
By FWhoop Xelqua 2023-04-04 16:47:07 0 26K
Business
What Were Their Most Significant Achievements?
Every great leader leaves behind a legacy shaped by their most meaningful accomplishments. By...
By Dacey Rankins 2025-06-27 16:38:15 0 5K

BigMoney.VIP Powered by Hosting Pokrov