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.

Search
Categories
Read More
Business
Why Is Mentoring Important? Unlocking Growth Through Meaningful Connections
Mentoring is far more than just a professional relationship—it’s a powerful...
By Dacey Rankins 2025-05-22 20:11:31 0 8K
Marketing and Advertising
What Are the Disadvantages of Newspaper Advertising?
While newspaper advertising offers credibility, trust, and local reach, it is not without...
By Dacey Rankins 2026-01-23 19:22:42 0 2K
Business and Corporate Finance
How a CFO Helps With Fundraising, Investor Relations, Forecasting, and Financial Storytelling
How a CFO Helps With Fundraising, Investor Relations, Forecasting, and Financial Storytelling...
By Leonard Pokrovski 2026-01-14 16:17:56 0 2K
Marketing and Advertising
How to Promote Your Small Business on Facebook (Organic + Ads Strategy)
Introduction Facebook is still one of the most powerful platforms for small business promotion....
By Dacey Rankins 2025-10-03 20:42:34 0 7K
Социальные проблемы
Большой побег. The Great Escape. (1963)
Американские, британские и канадские военнопленные осуществляют массовый побег из немецкого...
By Nikolai Pokryshkin 2023-02-21 11:42:09 0 30K

BigMoney.VIP Powered by Hosting Pokrov