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.

Pesquisar
Categorias
Leia Mais
Business
How Long Do Coaching Engagements Last?
Coaching is a personalized process designed to help individuals grow, gain clarity, and achieve...
Por Dacey Rankins 2025-07-22 11:00:05 0 7K
Business and Society
Business in Our Current Society: Navigating the Modern Landscape
In today’s fast-paced and interconnected world, business is more dynamic and complex than...
Por Dacey Rankins 2025-01-23 15:03:20 0 18K
Business
How Do I Determine the Right Price for My Product?
Pricing is one of the most important strategic decisions any business makes. Set your price too...
Por Dacey Rankins 2025-09-02 15:43:01 0 5K
Телевидение
Accent TV. Молдавия.
Accent TV - телеканал, освещающий новости политики, общества, культуры и спорта в Молдове. Помимо...
Por Nikolai Pokryshkin 2022-11-19 15:30:10 0 43K
Business
What Are the Benefits and Risks of Entrepreneurship?
Entrepreneurship is an exciting journey that offers financial independence, creativity, and the...
Por Dacey Rankins 2025-03-12 14:41:16 0 11K

BigMoney.VIP Powered by Hosting Pokrov