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
Marketing and Advertising
How Often Should a Radio Ad Run?
One of the most important questions in radio advertising is not just where or how to advertise,...
By Dacey Rankins 2026-02-09 16:52:16 0 276
Marketing and Advertising
Can I Make Money Blogging? (The Complete Guide to Monetizing Your Blog)
Many aspiring bloggers ask the same question: 👉 Can blogging actually make money, or is it just...
By Dacey Rankins 2025-09-23 17:12:24 0 6K
Business
How do I effectively interact with my audience to build a loyal community?
Building a loyal community is a key component of long-term success in any business, brand, or...
By Dacey Rankins 2025-02-20 16:48:36 0 14K
Business
How Do I Find the Right Mentor or Coach?
Finding the right mentor or coach can be a transformative step in your personal or professional...
By Dacey Rankins 2025-07-09 14:22:21 0 7K
Business
How Can Social Media Help Business Growth?
In the modern digital economy, businesses of all sizes—from local startups to global...
By Dacey Rankins 2025-09-05 15:36:49 0 12K

BigMoney.VIP Powered by Hosting Pokrov