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 Practice Classes in JavaScript Practicing Classes Instantiating an Object Solution

Chris Shaffer
Chris Shaffer
12,030 Points

Please clarify - can class constructor methods shared parameter namespace with custom methods of the same class?

This is the original question:

Challenge Task 1 of 2 Add a method to the User class called changeUsername()

This method should receive one parameter username, a string representing a new username for the user1 object. This method should not return anything Inside the method, update the value of the username property to the value of the username parameter"

I believe what is being asked is to accept a parameter in the NEW method (not the parameter already accepted by the class constructor method) that will be used to update the username property.

Otherwise, what is being done is to use the new changeUsername method to update the username to it's current value, which makes no sense.

I'm not clear if in context when username is already a parameter on the constructor method if this conflicts with using it again as a parameter on the new changeUsername method. It's confusing whether this is in the same namespace or not.

In my day job I would not reuse parameter naming within the same function unless intending to use the same literal original parameter value - it's confusing to read. Instead I would use 'newUserName' or something.

Maybe someone can clarify if this is necessary in this instance or if the constructor and other methods do/don't share the same parameter namespace.

Either way, my answer looked like this and it was accepted:

class Student {
  constructor(username) { 
    this.username = username;
    ...
    // just skipping ahead here - yes, I had more code in the constructor
 }


  changeUsername(newUserName) {
    this.username = newUserName;
  }
}

2 Answers

Steven Parker
Steven Parker
231,248 Points

The scope of a parameter is confined to the method in which it is defined. So using the same name for a parameter in two different methods causes no conflicts.

Using the same name makes sense here, because the value is used exactly the same way in both methods. Still, there's nothing wrong with giving them unique names if it makes the code more readable for you.

Chris Shaffer
Chris Shaffer
12,030 Points

I updated the title to be more accurate based on the clarification.

Chris Shaffer
Chris Shaffer
12,030 Points

Thanks again, Steven. Great clarification.