JavaScript set()

0
12χλμ.

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.

Αναζήτηση
Κατηγορίες
Διαβάζω περισσότερα
Business
How Do I Overcome Fear of Public Speaking?
Public speaking is one of the most common fears in the world — and it affects people of all...
από Dacey Rankins 2025-12-10 17:38:22 0 10χλμ.
Business
What Is Cloud Storage?
Most people never think about where their data lives. A photo is taken. A document is saved. A...
από Dacey Rankins 2026-06-05 19:40:17 0 1χλμ.
Accounting
Understanding Accounting: The Backbone of Financial Management
Accounting is often referred to as the "language of business" because it plays a critical role in...
από Dacey Rankins 2025-01-21 14:30:53 0 23χλμ.
Marketing and Advertising
What Skills Are Essential for a CMO?
The role of the Chief Marketing Officer (CMO) is one of the most complex in modern business....
από Dacey Rankins 2025-09-25 15:37:03 0 4χλμ.
Научная фантастика и фэнтези
Властелин колец: Возвращение короля. The Lord of the Rings: The Return of the King. (2003)
Повелитель сил тьмы Саурон направляет свою бесчисленную армию под стены Минас-Тирита, крепости...
από Nikolai Pokryshkin 2022-11-03 20:37:27 0 48χλμ.

BigMoney.VIP Powered by Hosting Pokrov