JavaScript set()

0
7χλμ.

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
YouTube Podcasts – A Trend That Determines Business Success
Podcasts as a global trend are currently on the rise and for good reason. We are talking about...
από Dacey Rankins 2024-09-05 18:59:16 0 17χλμ.
Искусство, культура и развлечения
Пол Смит: Джентльмен и Дизайнер. Paul Smith: Gentleman and Designer. (2011)
Провокационный портрет известного английского дизайнера Пола Смита. У него 400 магазинов и...
από Nikolai Pokryshkin 2023-06-04 14:02:11 0 27χλμ.
Gaelic
Gaelic: Unveiling the Rich Heritage and Enduring Legacy of Ireland's Indigenous Culture
  Gaelic: Unveiling the Rich Heritage and Enduring Legacy of...
από Leonard Pokrovski 2024-06-25 16:50:48 0 17χλμ.
Social Issues
Hillbilly Elegy (2020)
Based on the bestselling memoir by J.D. Vance, HILLBILLY ELEGY is a modern exploration of the...
από Leonard Pokrovski 2022-10-09 18:30:12 0 21χλμ.
Social Issues
Thirteen. (2003)
A thirteen-year-old girl's relationship with her mother is put to the test as she discovers...
από Leonard Pokrovski 2023-04-28 19:32:50 0 21χλμ.
image/svg+xml


BigMoney.VIP Powered by Hosting Pokrov