Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Phuc Le
Phuc Le
5,182 Points

What does the EventEmitter.call(this) mean in profile.js?

There are 2 statements in profile.js which are complex for me

util.inherits( Profile, EventEmitter );: In my opinion, it is used to inherit the prototype methods from EventEmitter into Profile, so we can call on method like this:

let profile = new Profile();

profile.on('data', callback{...});

Is that correct?

But I don't know what does EventEmitter.call(this) mean? Because I tried to delete this statement and the project still works well.

3 Answers

Phuc Le
Phuc Le
5,182 Points

Could somebody give me a answer?

David Mendiola
seal-mask
.a{fill-rule:evenodd;}techdegree
David Mendiola
Full Stack JavaScript Techdegree Student 16,579 Points

.call is a prototype available to all JS functions. It specifies what to use for "this" followed by any arguments for the function. This example taken from mdn.

function greet() {
  var reply = [this.person, 'Is An Awesome', this.role].join(' ');
  console.log(reply);
}

var i = {
  person: 'Douglas Crockford', role: 'Javascript Developer'
};

greet.call(i); // Douglas Crockford Is An Awesome Javascript Developer
Butler Fuqua
Butler Fuqua
5,643 Points

I have the same question. I don't really see what it means, or why it is necessary. Also, what does the code above it mean? it looks to be in comments, but my syntax highlighter has highlighted it.

/**
 * An EventEmitter to get a Treehouse students profile.
 * @param username
 * @constructor
 */