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.

Site içinde arama yapın
Kategoriler
Read More
Business
How Can I Secure Funding for My Startup?
Securing funding is one of the most crucial and challenging aspects of starting a new business....
By Dacey Rankins 2025-03-04 14:57:39 0 14K
Marketing and Advertising
Common Marketing Mistakes (and How to Avoid Them)
Introduction: Why So Many Marketing Campaigns Fail Marketing is both an art and a science. It...
By Dacey Rankins 2025-10-14 17:04:07 0 2K
Телевидение
«Охотник и рыболов» - ТВ прямой эфир.
«Охотник и рыболов» - отличный выбор для тех, кому нравится природа, и просто для...
By Nikolai Pokryshkin 2022-10-27 12:03:57 0 35K
Business
How Do Startup Founders Pay Themselves?
As a startup founder, one of the most crucial aspects of building a business is determining how...
By Dacey Rankins 2025-04-02 16:35:40 0 11K
EĞİTİM BİLGİLERİ
The Role of References in Education: Guiding Learning, Honoring Scholarship
The Role of References in Education: Guiding Learning, Honoring Scholarship Education is a...
By Leonard Pokrovski 2024-05-18 19:53:27 0 19K

BigMoney.VIP Powered by Hosting Pokrov