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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Samuel Kleos](https://uploads.teamtreehouse.com/production/profile-photos/11699126/micro_Square_Photo_-_Tahir.jpg)
Samuel Kleos
Front End Web Development Techdegree Student 13,118 PointsWhy 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](https://uploads.teamtreehouse.com/production/profile-photos/11666882/micro_Rohald-profile.jpg)
Rohald van Merode
Treehouse StaffHey 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 🙂
Samuel Kleos
Front End Web Development Techdegree Student 13,118 PointsSamuel Kleos
Front End Web Development Techdegree Student 13,118 PointsThanks Rohald!