JavaScript set()

0
12Кб

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.

Поиск
Категории
Больше
Business
How can I generate passive income?
In today’s world, the pursuit of financial freedom has led many individuals to explore...
От Dacey Rankins 2025-01-15 13:40:05 0 22Кб
Business
How Can I Validate My Business Idea Without Significant Funding?
Validating a business idea is an essential step in the entrepreneurial process, but many aspiring...
От Dacey Rankins 2025-03-31 15:51:14 0 20Кб
Personal Development
What are personal development quotes?
What Are Personal Development Quotes? Personal development quotes are short, powerful statements...
От Michael Pokrovski 2026-02-12 14:48:46 0 3Кб
Productivity
How do I manage my time better?
How Do I Manage My Time Better? Time is one of the most valuable resources we have, yet it often...
От Michael Pokrovski 2026-02-16 15:50:10 0 9Кб
Business
What Are the Different Types of Influencers?
In the world of social media and digital marketing, influencers play a crucial role in shaping...
От Dacey Rankins 2025-02-27 16:37:34 0 12Кб

BigMoney.VIP Powered by Hosting Pokrov