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 Entrepreneurs Deal with Failure?
Failure is an inevitable part of the entrepreneurial journey. Despite their best efforts, many...
By Dacey Rankins 2025-03-17 18:43:24 0 10K
Rehber
1917. (2019)
April 6th, 1917. As an infantry battalion assembles to wage war deep in enemy territory, two...
By Leonard Pokrovski 2023-02-01 14:54:03 0 26K
Personal Finance
What Tax Bracket Am I In? How Tax Brackets Work and What They Really Mean
What Tax Bracket Am I In? How Tax Brackets Work and What They Really Mean Tax season often...
By Leonard Pokrovski 2025-11-20 16:59:08 0 1K
Business
What Are the Best Books for Startup Founders?
Starting a business is an exciting yet challenging journey, and as a startup founder, you're...
By Dacey Rankins 2025-04-03 14:54:40 0 8K
Marketing and Advertising
What Is Content Marketing and Why Does It Matter?
Content marketing has become one of the most powerful ways for businesses to attract, engage, and...
By Dacey Rankins 2025-11-03 15:04:56 0 3K

BigMoney.VIP Powered by Hosting Pokrov