JavaScript set()

0
12KB

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.

Suche
Kategorien
Mehr lesen
Marketing and Advertising
What Is the Difference Between Remarketing and Retargeting? A Complete Marketing Guide
In digital marketing, the terms remarketing and retargeting are often used interchangeably. Many...
Von Dacey Rankins 2026-02-10 22:18:06 0 3KB
Искусство, культура и развлечения
Моя поп-группа. La mia banda suona il pop. (2020)
Сюжет этой убойной итальянской комедии разворачивается вокруг одного очень крупного русского...
Von Nikolai Pokryshkin 2022-10-02 18:05:09 0 31KB
Business
What Are the Best Lead Generation Methods?
There is no single “best” lead generation method that works for every business,...
Von Dacey Rankins 2025-12-16 20:29:40 0 7KB
Marketing and Advertising
What Does a PR Professional Do? Roles, Skills, and Impact Explained
Introduction: The Unsung Heroes of Brand Reputation Every successful brand has one thing in...
Von Dacey Rankins 2025-10-24 16:05:56 0 5KB
Business
In E‑Commerce: “Did We Ship Quickly? Were Returns Easy?”
In the fast-paced world of e-commerce, speed and simplicity can make or break the customer...
Von Dacey Rankins 2025-07-25 14:11:15 0 8KB

BigMoney.VIP Powered by Hosting Pokrov