JavaScript set()

0
11KB

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.

Suche
Kategorien
Mehr lesen
Business
What Skills Are Essential for a Product Manager?
Product management requires a unique blend of technical, strategic, and interpersonal skills....
Von Dacey Rankins 2025-08-15 20:32:44 0 3KB
Human Resources
What Are Collateral Resources: Definition and Examples
Section 1: Defining Collateral Resources We can think of collateral resources as a set of keys...
Von Dacey Rankins 2024-03-12 16:00:28 0 34KB
Homeowners
The Modern Homeowner: Challenges, Opportunities, and Responsibilities
Homeownership is often considered a milestone, a symbol of stability and financial success. For...
Von Dacey Rankins 2024-12-02 14:19:50 0 11KB
Business
Do Incubators Take Equity in Startups?
Startup incubators provide a wide range of valuable services—from mentorship and networking...
Von Dacey Rankins 2025-04-24 17:12:41 0 15KB
Искусство, культура и развлечения
Манк. Mank. (2020)
1940 год. Нью-йоркский острослов Герман Манкевич, когда-то приехавший покорять Голливуд и в целом...
Von Nikolai Pokryshkin 2022-09-25 21:17:14 0 26KB

BigMoney.VIP Powered by Hosting Pokrov