Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

240
Vistas
Increment an object count variable by one, every time one of the many object methods is invoked/called

Let's say we have the following object:

class Foo{
     constructor(){
          this.count = 0;
     }
     method1() { <does something> }
     method2() { <does something else> }
     method3() { <does something even more> }
}

Now obviously we can add the following line of code to EACH method and the count will update:

this.count++;

While I know it's only one line code one can say that it is a redundant line of code. So is there way that we can detect if an object method has been invoked, no matter which one it is, and then update the count?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You might iterate over a separate object of methods to add instead, and have them do this.count++ first.

class Foo{
     constructor(){
          this.count = 0;
     }
}
const methods = {
  method1() {
    console.log('m1');
  },
  method2() {
    console.log('m2');
  },
  method3() {
    console.log('m3');
  },
};
for (const [key, value] of Object.entries(methods)) {
  Foo.prototype[key] = function(...args) {
    this.count++;
    value(...args);
  }
}

const f = new Foo();
f.method1();
f.method1();
f.method2();
console.log(f.count);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda