Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Build a Simple Dynamic Site with Node.js Handling Routes in Node.js Populating User Information

Samuel Kleos
seal-mask
.a{fill-rule:evenodd;}techdegree
Samuel Kleos
Front End Web Development Techdegree Student 13,483 Points

Why does Profile class use the .call and .emit methods and why is it is assigned 'this'.

var EventEmitter = require("events").EventEmitter;
function Profile(username) {
    // ❓👇🏽 What is the purpose of this .call method?
    EventEmitter.call(this);
    // ❓👇🏽 A reference to the object is created to emit 'end', 'data', and 'error'. 
    // Why not just use 'this' instead to refer to the Profile object?
    profileEmitter = this;

    //Connect to the API URL (https://teamtreehouse.com/username.json)
    var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
        var body = "";

        if (response.statusCode !== 200) {
            request.abort();
            //Status Code Error
            profileEmitter.emit("error", new Error("There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"));
        }

        //Read the data 
        response.on('data', function (chunk) {
            body += chunk;
            profileEmitter.emit("data", chunk);
        });

        response.on('end', function () {
            if(response.statusCode === 200) {
                try {
                    //Parse the data
                    var profile = JSON.parse(body);
                    profileEmitter.emit("end", profile);
                } catch (error) {
                    profileEmitter.emit("error", error);
                }
            }
        }).on("error", function(error){
            profileEmitter.emit("error", error);
        });
    });
}

studentProfile.on("end", console.dir); // here I've included how its printed to console.

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hey Samuel Kleos,

The call method is executed when the Profile instance is created, it appends the properties from the EventEmitter constructor to the Profile. Which for example will give you the emit method on the Profile class.

As for the second comment regarding storing this in a profileEmitter variable, this is just a preference. You could indeed use this throughout the code but giving it a name like profileEmitter makes it more descriptive.

Hope this answers your question 🙂