JavaScript set()

0
12K

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
Productivity
How can I be more productive every day?
How Can I Be More Productive Every Day? Productivity is often misunderstood as simply...
By Michael Pokrovski 2026-03-26 23:38:20 0 13K
Business
Fake Ad Blocker Capable of Running Arbitrary code on Windows Systems
ESET specialists have discovered an advertising malware that is distributed under the guise of an...
By Dacey Rankins 2024-07-25 10:04:51 0 16K
Business and Corporate Finance
What Are the Best Financing Options for Small Businesses?
What Are the Best Financing Options for Small Businesses? Banks, Online Lenders, and Government...
By Leonard Pokrovski 2026-01-07 07:22:46 0 5K
Marketing and Advertising
How Can I Reduce YouTube Advertising Costs?
Advertising on YouTube can be highly effective—but without proper strategy, costs can...
By Dacey Rankins 2026-03-04 18:27:24 0 7K
Business
What Methods Are Used in Inventory Control?
Common: EOQ, ABC Analysis, FIFO/LIFO Pricing, Safety Stock, and Forecasting Tools Effective...
By Dacey Rankins 2025-07-03 18:01:07 0 9K

BigMoney.VIP Powered by Hosting Pokrov