JavaScript set()

0
8χλμ.

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
BUSINESS MANAGEMENT: 12 STEPS TO EFFICIENCY
BUSINESS MANAGEMENT: 12 STEPS TO EFFICIENCY In modern business, the key element of success...
από Leonard Pokrovski 2024-08-11 15:16:16 0 14χλμ.
Activism
The Power of Activism: Driving Change in Society
Activism is the practice of taking action to bring about social or political change. It...
από Dacey Rankins 2024-10-14 18:30:40 0 15χλμ.
Aging
Shock, awe and suicide. How the Genetic Program Drives People to the Grave
The biggest threat to humans is biological aging, that is, the slow decline of bodily functions,...
από Dacey Rankins 2024-04-11 16:26:11 0 11χλμ.
Financial Services
Price elasticity of demand and price elasticity of supply
Key points Price elasticity measures the responsiveness of the quantity...
από Mark Lorenzo 2023-02-14 14:37:48 0 10χλμ.
Social Issues
Barry Lyndon. (1975)
An Irish rogue wins the heart of a rich widow and assumes her dead husband's aristocratic...
από Leonard Pokrovski 2023-03-29 19:57:20 0 23χλμ.

BigMoney.VIP Powered by Hosting Pokrov