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.

Rechercher
Catégories
Lire la suite
Business
How Long Does It Take to See Retention Improvements?
When companies start investing in user retention strategies, one of the first questions...
Par Dacey Rankins 2025-09-16 16:32:29 0 6KB
Business
How Do I Handle Negative Feedback?
Strategies to Receive and Respond to Criticism Positively and Productively Receiving negative...
Par Dacey Rankins 2025-05-28 14:20:11 0 9KB
Life Issues
Life Of Brian (1979)
Born on the original Christmas in the stable next door to Jesus Christ, Brian of Nazareth spends...
Par Leonard Pokrovski 2023-06-30 19:15:42 0 24KB
Business
Crowdfunding for Business: What It Is, How It Works, and What Are the Crowdfunding Platforms for Projects
Crowdfunding for Business: What It Is, How It Works, and What Are the Crowdfunding Platforms for...
Par Leonard Pokrovski 2024-08-01 21:59:41 0 19KB
Business
What Is the Difference Between a Lead and a Prospect?
The terms lead and prospect are often used interchangeably — but they are not the same...
Par Dacey Rankins 2025-12-16 21:26:59 0 2KB

BigMoney.VIP Powered by Hosting Pokrov