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.

Cerca
Categorie
Leggi tutto
Marketing and Advertising
Is Radio Advertising Still Relevant in 2026? A Modern Marketing Perspective
With the rise of social media, streaming platforms, artificial intelligence, and immersive...
By Dacey Rankins 2026-02-10 22:11:34 0 6K
Business
How Much Does It Cost to Buy a Franchise?
A man once told me he bought a franchise because he was tired of uncertainty. Three years later,...
By Dacey Rankins 2026-05-29 17:24:22 0 4K
Business
How Do Onboarding and User Experience Impact Retention?
Retention is a key metric for any product or service. However, user retention doesn’t...
By Dacey Rankins 2025-09-16 16:35:18 0 11K
Business and Corporate Finance
What Is Business Finance? How Companies Manage Money
What Is Business Finance? How Companies Manage Money Introduction Every business, whether it is...
By Leonard Pokrovski 2025-12-27 22:39:19 0 8K
Business
What Are Home-Based Franchise Opportunities?
For decades, entrepreneurship came with a familiar image. A storefront. A lease agreement. A...
By Dacey Rankins 2026-06-04 00:37:13 0 2K

BigMoney.VIP Powered by Hosting Pokrov




google-site-verification=q_0zhvNWwTfwbQ-uEJsvcmUKKbtOWUTgl3H57qDiFrg