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
Business
How Do Etiquette Rules Differ Between Formal and Informal Settings?
Etiquette is often misunderstood as a rigid set of rules — a collection of do’s and...
Por Dacey Rankins 2025-11-25 10:50:08 0 3KB
Marketing and Advertising
How Do I Align Marketing With Sales and Understand the Buyer’s Journey?
One of the most common challenges in B2B marketing is the disconnect between marketing and sales...
Por Dacey Rankins 2025-09-20 15:38:59 0 5KB
Жизненные вопросы
Останься со мной. Stand by Me. (1986)
Действие фильма переносит зрителя в 50-е годы. Лето подходит к концу, но сезон развлечений...
Por Nikolai Pokryshkin 2023-05-08 16:23:14 0 35KB
Business
How Do I Start a Business?
Starting a business can be one of the most exciting and rewarding endeavors of your...
Por Dacey Rankins 2025-04-30 12:48:04 0 10KB
Business
Can AI improve demand forecasting in industries like fashion?
In the fast-paced world of fashion, accurately predicting demand can be a significant challenge....
Por Dacey Rankins 2025-01-13 14:43:16 0 20KB

BigMoney.VIP Powered by Hosting Pokrov