JavaScript set()

0
10K

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.

Buscar
Categorías
Read More
Financial Services
Explicit and implicit costs and accounting and economic profit
Key points Privately owned firms are motivated to earn profits. Profit is the...
By Mark Lorenzo 2023-07-17 19:32:29 0 14K
Социальные проблемы
Олдбой. Oldboy. (2003)
1988 год. Обыкновенный и ничем непримечательный бизнесмен О Дэ-cу в день трёхлетия дочери по пути...
By Nikolai Pokryshkin 2022-12-26 12:04:09 0 24K
Antiques
Antiques and Nobility
One of the most interesting definitions of antiques dates back to the end of the XIX century and...
By FWhoop Xelqua 2022-09-27 11:06:24 0 36K
Programming
Chrome Extensions Using Python
The idea is to compile Python to Javascript (technically a JS pre-compiler)...
By Jesse Thomas 2023-03-20 20:46:37 0 10K
Directorio
Casablanca. (1942)
A cynical expatriate American cafe owner struggles to decide whether or not to help his former...
By Leonard Pokrovski 2022-12-05 19:06:06 0 23K

BigMoney.VIP Powered by Hosting Pokrov