JavaScript set()

0
11K

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
Телевидение
Инфо 24 - ТВ прямой эфир.
Телеканал «ИНФО 24» начал свое вещание в 2011 году.В 2022 году получил статус...
By Nikolai Pokryshkin 2022-11-03 19:53:04 0 29K
Financial Services
Indexing and its limitations
Key points A payment is said to be indexed if it is automatically...
By Mark Lorenzo 2023-03-06 20:56:29 0 13K
Business
How Do I Create a Business Plan for a Startup?
Entrepreneurs seek tips and examples tailored to startups when creating a business plan. A...
By Dacey Rankins 2024-12-18 16:56:58 0 25K
Business
What Does a Sales Manager Do?
A sales manager is one of the most influential roles in any revenue-driven organization. While...
By Dacey Rankins 2025-12-18 20:29:52 0 4K
Programming
Python UnicodeError
In Python, Unicode standards have two types of error: Unicode encodes error and Unicode decode...
By Jesse Thomas 2023-03-31 20:38:35 0 12K

BigMoney.VIP Powered by Hosting Pokrov