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 JavaScript Foundations Objects Prototypes

Javascript Challenge on prototype

I just don't understand this at all. I watched the video over and over. Here is the challenge question.

Modify the 'Car' constructor function so that it takes an argument for its model and set its 'model' to the model passed in from the argument.

<script>

  var carPrototype = {
    model: "generic",
    currentGear: 0,
    increaseGear: function() {
      this.currentGear ++;
    },
    decreaseGear: function() {
      this.currentGear--;
    } 
  }

  function Car() {

  }


</script>

4 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Gary;

Great question. I'll walk you through my thought process, your mileage may vary. :)

set its => that indicated to me that I would be using this.???

'model' => this.model

to the model passed in from the argument => that was model as well so we need to set this.model equal to model. Which is this.model = model.

I too get confused when variable and argument names are close to or the same as each other. We could have designed the function to be slightly different (and it would still pass the challenge and function, no pun intended, the same), like:

function Car(argModel) {
    this.model = argModel;
}

To me that helps clear up which word is being used where, but it doesn't help with writing clear and meaningful code in my mind. So going with model is better, for me, than argModel. But it is just a name, you could have used something else as well.

Sorry for the rambling, hope it didn't wind up confusing you more.

Ken

Thanks Ken. You broke this down really nicely. The last step is confusing to me by the way its worded. " set its 'model' to the model passed in from the argument." That doesn't make me think to use this.model = model. Do you think you have a way of explaining that to me to better understand?

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Gary;

Greetings! Let's see if we can get you headed in the correct direction!

Challenge, Task 1:

Modify the 'Car' constructor function so that it takes an argument for its model and set its 'model' to the model passed in from the argument.

First, we have a constructor function called Car defined like so:

function Car() {

}

Second, make it so it takes an argument for it's model...

function Car(model) {

}

Last, set it's 'model' to the model passed in from the argument.

function Car(model) {
     this.model = model;
}

Does that make any sense? I hope it helps. I find that on many of the challenges breaking down the task like this helps to clarify things.

Post back with any additional questions.

Happy coding,

Ken

I understand Ken. I appreciate you. Thank you

Ken Alger
Ken Alger
Treehouse Teacher

Pleased to help. Keep up the great work and happy coding!

Ken