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

hakan Gülay
hakan Gülay
8,489 Points

ı couldnt understand

Hi,

http://teamtreehouse.com/library/prototypes

actually ı solved this questions but ı couldnt understand how "this" which is in Car can take CarPrototype ? ok we use Car.prototype=CarPrototype; to add but ı dont understand the logic of it

Rui Bi
Rui Bi
29,853 Points

Car.prototype = CarPrototype is pretty much you saying to Javascript, "Hey, I want you to take all car objects I have made as well as all car objects I have in the future, and make them have the fields and methods of CarPrototype.

When you say this.field = value inside the constructor or in a method, you are setting a specific field in that specific instance of the object to the given value.

A prototype is kind of a template that you can apply to all objects of a given class. When you are applying a prototype to a object. The this keyword refers to the specific instance of the object you are working with.

If you a familiar with other programming languages, you can think of an object's prototype as a static variable, which is the same for all objects of the class. On the other hand, variables that are usually referenced with the this keyword are object fields, which can have different values for different instances of the object.