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
Personal Development
How do I improve leadership skills?
How Do I Improve Leadership Skills? Leadership is not limited to job titles or management...
Par Michael Pokrovski 2026-02-10 20:10:31 0 2KB
Personal Finance
What is the interest rate on student loans? — Current federal rates and typical private loan ranges
What is the interest rate on student loans? — Current federal rates and typical private...
Par Leonard Pokrovski 2025-12-16 21:14:14 0 4KB
Cultures and Groups
Art in Culture: A Timeless Reflection of Society
Art has long been an essential element of human civilization, playing a profound role in shaping...
Par Dacey Rankins 2025-01-22 14:28:16 0 14KB
Business
What Are Effective Brainstorming Techniques for Startup Ideas?
Coming up with the right idea is one of the most crucial steps in launching a successful startup....
Par Dacey Rankins 2025-03-24 15:19:06 0 9KB
Television
Silverbird Live TV. Nigeria.
Silverbird TV (Silverbird Television) — local entertainment channel of Lagos, southwest...
Par Nikolai Pokryshkin 2022-11-27 19:54:52 1 39KB

BigMoney.VIP Powered by Hosting Pokrov