JavaScript set()

0
12K

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.

Pesquisar
Categorias
Leia Mais
Human Resources
How Does HR Recruit Employees?
Recruiting the right employees is one of the most important responsibilities of Human Resources...
Por Dacey Rankins 2026-03-19 18:34:17 0 2K
Business
How Do I End a Speech?
Ending a speech is just as important as starting one — maybe even more.Your conclusion is...
Por Dacey Rankins 2025-12-10 17:44:59 0 3K
Marketing and Advertising
How Do You Measure the Success of a Community?
Community building is an art, but measuring whether your community is truly successful requires...
Por Dacey Rankins 2025-09-26 15:12:18 0 12K
Life Issues
Life Of Brian (1979)
Born on the original Christmas in the stable next door to Jesus Christ, Brian of Nazareth spends...
Por Leonard Pokrovski 2023-06-30 19:15:42 0 25K
Business and Corporate Finance
What Are the Best Financing Options for Small Businesses?
What Are the Best Financing Options for Small Businesses? Banks, Online Lenders, and Government...
Por Leonard Pokrovski 2026-01-07 07:22:46 0 5K

BigMoney.VIP Powered by Hosting Pokrov