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

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.