Use variable from one class to another

Asked

Viewed 55 times

2

I have the following method:

    this.retorno.then(function(result){
        let a = result[0].infos.inProgress; 
        var cards = [];
        a.forEach(b => { 
            let card = new Card(b.name, b.type, b.date, b.id);
            cards.push(card);
        });

        let divOpen = document.querySelector("#open");
        let cardView = new CardView(divOpen);
        cardView.load(cards);

    });

It itself is working perfectly, but I would like to use the variable cards that contains an array of objects in another method of the same class. How can I do that?

  • 1

    It’s not just about sticking to it this as he did with this.resultado?

  • Livia, there’s no need to say it’s a beginning question, it’s irrelevant to doubt.

1 answer

0


Hello,

you can create an attribute called cards as in the example below

//um novo atributo na sua classe
cards = [];

this.retorno.then(function(result){
    let a = result[0].infos.inProgress; 
    var cards = [];
    a.forEach(b => { 
        let card = new Card(b.name, b.type, b.date, b.id);
        cards.push(card);
    });

    //fiz dessa forma, por que não se pode usar 'this' dentro de arrow functions
    //, por conta do escopo.
    this.cards = cards;

    let divOpen = document.querySelector("#open");
    let cardView = new CardView(divOpen);
    cardView.load(cards);

 });
}

doing so, you can use the this cards in another method.

about scope of functions Arrow, take a look at this post, very good by the way: https://blog.da2k.com.br/2019/01/07/javascript-tudo-sobre-arrow-functions/

I hope I’ve helped in some way

  • It worked perfectly, it was so simple! Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.