JavaScript set()

0
11KB

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.

Pesquisar
Categorias
Leia mais
Financial Services
Microeconomics
Key points Demand curves can shift. Changes in factors like average income and...
Por Mark Lorenzo 2023-01-27 15:38:59 0 12KB
Business
Product Career Strategy: How to Become a CPO Through Professional Brand Development
Product Career Strategy: How to Become a CPO Through Professional Brand Development...
Por Leonard Pokrovski 2024-08-27 16:05:41 0 26KB
Internet
Google Ads, part 9: How to Create an Advertisment
part 8: Google Ads : Mectrics (bigmoney.vip) Two basic elements of ads to start with... Heading...
Por FWhoop Xelqua 2023-02-15 11:53:19 0 19KB
Искусство, культура и развлечения
Аннетт. Annette. (2021)
Действие развернётся в Лос-Анджелесе. По сюжету Генри работает стендап-комиком, а его супруга Энн...
Por Nikolai Pokryshkin 2022-09-03 21:19:35 0 48KB
Marketing and Advertising
How Does B2C Marketing Differ from B2B Marketing?
Marketing is not one-size-fits-all. While many principles overlap, B2C (Business-to-Consumer) and...
Por Dacey Rankins 2025-09-20 16:38:55 0 3KB

BigMoney.VIP Powered by Hosting Pokrov