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

153
Vistas
Pass a function that uses other functions as parameter in Javascript

I am developing a project in Typescript that has two classes like this:

class A{

  //some attributes

  function_to_pass(){
    //Do something
    this.someFunction();
    //Make changes in object A
  }

  protected someFunction(){
    //Do some stuff
  }

}
class B{
  
  private readonly _functionProvided: () => void;

  constructor(functionProvided: () => void){
    this._functionPassed = functionProvided;
  }

  private myFunction(){
    //Do some stuff
    this._functionProvided();
  }

}

I want the object of class B to call the "function_to_pass" method in the object of class A, so i pass the function as a parameter in the constructor and then call "myFunction" in this object b. Like this

objectA = new A();

objectB = new B(objectA.function_to_pass)

objectB.myFunction()

But, i get the following error:

TypeError: this.someFunction is not a function

I suppose that objectB does not know about objectA, so objectB can't manipulate objectA.

Is there anyway i can do this?

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

0

I see two problems with the code, the first I think is a typo: the class B constructor doesn't initialize the instance data, _functionProvided. (see FIX #1)

The second problem is the real problem, and has to do with the execution context of function_to_pass. In order that this be defined when it runs, the function must be bound to its instance. See (FIX #2).

class A{

  //some attributes

  function_to_pass(){
    //Do something
    this.someFunction();
    //Make changes in object A
  }

  protected someFunction(){
    //Do some stuff
  }

}

class B{
  
  private readonly _functionProvided: () => void;

  constructor(functionProvided: () => void){
    // FIX #1, no such thing as _functionPassed
    // WAS: this._functionPassed = functionProvided;
    this._functionProvided = functionProvided;
  }

  private myFunction(){
    //Do some stuff
    this._functionProvided();
  }

}


let objectA = new A();

// FIX #2: bind the receiver of function_to_pass
// WAS: objectB = new B(objectA.function_to_pass)
let objectB = new B(objectA.function_to_pass.bind(objectA));

objectB.myFunction()
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