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.

Căutare
Categorii
Citeste mai mult
Marketing and Advertising
What Agencies Were Founded by Famous Advertisers?
Some of the world’s most influential advertising agencies did not begin as large corporate...
By Dacey Rankins 2026-01-08 11:56:18 0 1K
Marketing and Advertising
What Factors Affect Search Engine Rankings?
A Complete Guide to the Key Elements That Influence SEO Performance in 2025 When it comes to...
By Dacey Rankins 2025-10-31 15:42:09 0 9K
Marketing and Advertising
Can I Make Money Blogging? (The Complete Guide to Monetizing Your Blog)
Many aspiring bloggers ask the same question: 👉 Can blogging actually make money, or is it just...
By Dacey Rankins 2025-09-23 17:12:24 0 6K
Marketing and Advertising
What Is a Remarketing Tag or Pixel? A Complete Guide to Tracking, Targeting, and Optimization
Every successful remarketing campaign begins with one essential element: the remarketing tag or...
By Dacey Rankins 2026-02-12 00:15:35 0 1K
Women's Health
Women Health
Women's health is the most valuable thing that every representative of the fair half of humanity...
By Dacey Rankins 2024-04-24 17:14:18 0 17K

BigMoney.VIP Powered by Hosting Pokrov