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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Britton Zirkle](https://secure.gravatar.com/avatar/df64955e058689e7241ec0bd7f481530?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Britton Zirkle
Courses Plus Student 15,005 PointsWhy is the Prototype necessary?
What is the purpose of using the prototype when you can use constructor notation to define the properties and methods you wish to use in instances of the object? for example
var personPrototype = {
name: 'Anonymous',
greet: function (name, mood) {
name = name || "You";
mood = mood || "good";
console.log("Hello, " + name + " I am " + this.name + " and I am in
a " + mood + " mood!");
},
species: 'Homo Sapien'
};
function Person (name) {
this.name = name;
}
Person.prototype = personPrototype;
jim = new Person("Jim");
as opposed to
function Person (name) {
this.name = name;
this.species = 'Homo Sapien';
this.greet = function (name, mood) {
name = name || "You";
mood = mood || "good";
console.log("Hello, " + name + " I am " + this.name + " and I am in
a " + mood + " mood!");
};
}
var jim = new Person('Jim');
Unless I am missing something, using the constructor notation seems to save a lot of code and time.
2 Answers
![eck](https://uploads.teamtreehouse.com/production/profile-photos/111095/micro_8516739396_bcc0fa09dd_b.jpg)
eck
43,038 PointsYou're right, you can declare your properties and methods in the constructor, and your code will work as expected. From my understanding though, you have small performance improvements when using the prototype to store common data and methods.
Check this stack overflow thread that basically answers the same question you have asked.
![Olga Filatova](https://secure.gravatar.com/avatar/b4b7bc3ce43ec5f6c4e51ef73a3239f2?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Olga Filatova
8,045 PointsNot only it's good for performance and cascading changes but also when using prototypes you can set one prototype to different classes or prototypes.
For example here we can declare prototypes like "american", "briton", "ukrainian", etc. linking to prototype "person" and for each inheriting prototype declare their own methods and properties. And so on, and so on.
Imagine which code will become larger in such circumstances.
In JS for example String, Number, Array, etc. prototypes inherit from Object prototype.
And winning on performance is not little. Especially in big JS projects. In having link to prototype in object or whole bunch of repeating code inserted in every little variable is a big differnence. DRY. Have mercy on RAM and your user. :)
Britton Zirkle
Courses Plus Student 15,005 PointsBritton Zirkle
Courses Plus Student 15,005 PointsErik,
Thanks for the info. You are correct. I also found out that using prototype allows it to act more like a class in which a change in the prototype will cascade to all objects created from that prototype which is pretty cool.