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

Jacob Tennyson
Jacob Tennyson
6,240 Points

Challenge Task 1 Modify the 'Car' constructor function so that it takes an argument for its model and set its 'mod

I really don't understand this?

index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title> JavaScript Foundations: Objects</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Objects: Prototypes</h2>
    <script>


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

      function Car() {

      }


    </script>
  </body>
</html>

1 Answer

Hey Jacob Tennyson,

The Car() function is setting up a constructor so that additional objects that have the prototype of carPrototype can be added. What you are doing is setting up the object to take in a new model value to replace the "generic" value within the default prototype. And since we don't know exactly what the new car is going to be called, we can use the this keyword to change the model for this car to the model variable we receive. You can use the "model" variable name twice with two different modes of functionality, because the left side of the equation is going to be the key "model" and the right side is going to be the variable placeholder "model" that you set inside the function.

I don't want to post the answer, because I want you to intuitively grasp the concept that is going on here. But, if you get too frustrated at it, I'll help you further!