M
M
Michael2016-11-17 21:41:43
JavaScript
Michael, 2016-11-17 21:41:43

How to properly deal with this in classes?

There is this class:

class ParserRSS {
    constructor(FeedParser, request) {
        this.feedParser = new FeedParser();
    }

    request(url) {
        const req = this.request(url);
        req.on('response', function(res) {
            this.pipe(this.feedParser);
        });
    }
}

Question in line: this.pipe(this.feedParser);. There, the first this from the callback should be, and the second should be a property of the class (which is set when the constructor is called). How can I make it clear to the code where this is?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
_
_ _, 2016-11-17
@mak_ufo

Both this in this case is the context of the .on callback

C
catHD, 2016-11-18
@catHD

https://learn.javascript.ru/bind
https://habrahabr.ru/post/149516/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question