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.

Cerca
Categorie
Leggi tutto
Business
Tips for Running Effective Meetings
Meetings are essential tools for collaboration, decision-making, and alignment. However, without...
By Dacey Rankins 2025-06-02 16:35:40 0 8K
Business
True Growth Hackers Are Cross-Functional Experts
The phrase “growth hacker” has become popular in modern business, often used to...
By Dacey Rankins 2025-09-12 20:01:49 0 2K
Business
How Can You Reduce Cart Abandonment Effectively?
Cart abandonment is a major challenge for e-commerce businesses. According to industry studies,...
By Dacey Rankins 2025-09-10 17:38:05 0 3K
Business
How Do You Calculate Conversion Rate?
Conversion Rate Optimization (CRO) is one of the most powerful tools for improving business...
By Dacey Rankins 2025-09-10 13:15:00 0 3K
Programming
Python Newline
In Python, the new line character “\n” is used to create a new line....
By Jesse Thomas 2023-03-01 22:08:02 0 11K

BigMoney.VIP Powered by Hosting Pokrov