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.

Site içinde arama yapın
Kategoriler
Read More
Personal Finance
What Happens if Social Security or State Pension Benefits Are Cut — or Aren’t Enough?
  What Happens if Social Security or State Pension Benefits Are Cut — or Aren’t...
By Leonard Pokrovski 2025-11-26 20:21:59 0 9K
Жизненные вопросы
Пролетая над гнездом кукушки. One flew over the cuckoo's nest. (1975)
Рэндла Патрика Макмёрфи, патологического преступника и бунтаря, переводят из колонии в...
By Nikolai Pokryshkin 2022-11-13 18:30:48 0 31K
Business
Is IaaS better than traditional servers?
There is a peculiar moment that occurs in many boardrooms. A company has outgrown its existing...
By Dacey Rankins 2026-06-05 03:19:40 0 5K
Marketing and Advertising
How Do I Become a Copywriter?
A Step-by-Step Guide to Building a Career in Persuasive Writing Copywriting is one of the most...
By Dacey Rankins 2025-09-30 16:02:38 0 8K
Business
What Are Common KPIs Across Different Departments?
Every organization runs on data. But not all departments measure success the same way. A sales...
By Dacey Rankins 2025-08-27 15:02:14 0 9K

BigMoney.VIP Powered by Hosting Pokrov