Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
Passing class method as callback to another class in javascript

I am trying to create a pop up menu (via a class) called from another class. After these inputs are filled in I want the input data to be processed by the object from which the pop up menu was initiated.

My thought process created the following code (simplified ofc):

class foo{
    constructor(){
        this.prroperty = 'yes';
    }

    eventMethod(){
        let eventForm = new bar();
        eventForm.fire(this.endMethod);
    }

    endMethod(values){
        // console.log(this);
        console.log(values + this.prroperty);
    }
}

class bar{
    constructor(){
        this.property = 'no';
    }

    fire(callback){
        let value = 'message';
        callback(value);
    }
}

let object = new foo();
object.eventMethod();

this doesn't work obviously because "this" now no longer referes to the foo created object. I'm planning on using the controller from the foo class and the popup menu created by the bar class a lot. Therefore I'm using classes instead of creating single objects.

My question is if there is a way to deliver and process the values while still being able to do so within classes instead of objects because i want to use them multiple times.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I changed the foo and bar to Form and Popup:

1) Quick — and possibly dirty — solution:

You can send this as the second argument to .fire():

class Form {
    constructor() {
        this.property = 'yes';
    }

    eventMethod() {
        let popup = new Popup();
        popup.fire(this.endMethod, this);
    }

    endMethod(values, that) {
        console.log(values + that.property);
    }
}

class Popup {
    constructor() {
        this.property = 'no';
    }

    fire(callback, that) {
        let value = 'message';
        callback(value, that);
    }
}

let form = new Form();
form.eventMethod();

2) Using .bind()

More robustly, you can use the .bind() method to bind the Form this to endMethod before sending it out:

class Form {
    constructor() {
        this.property = 'yes';
    }

    eventMethod() {
        let popup = new Popup();
        popup.fire(this.endMethod.bind(this));
    }

    endMethod(values) {
        console.log(this);
        console.log(values + this.property);
    }
}

class Popup {
    constructor() {
        this.property = 'no';
    }

    fire(callback) {
        let value = 'message';
        callback(value);
    }
}

let form = new Form();
form.eventMethod();

about 4 years ago · Juan Pablo Isaza Relatório

0

This is a simple possible solution. You can pass to the popup the whole "this" form instance

class Form {
    constructor() {
        this.property = 'yes';
    }

    eventMethod() {
        let popup = new Popup();
        popup.fire(this);
    }

    endMethod(values) {
        console.log(values + this.property);
    }
}

class Popup {
    constructor() {
        this.property = 'no';
    }

    fire(form) {
        let value = 'message';
        form.endMethod(value);
    }
}

let form = new Form();
form.eventMethod();

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda