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

What is the difference?

Actually, what is the advantages of prototyping?

// this
const obj = {
  name : "Treehouse",
  is : "Learning site",
  work = function() {
    console.log("Teaching in better way");
  }
} 
// and this
const obj2 = {
  name : "Treehouse",
  is : "Learning site"
}
obj.prototype.work = function() {
  console.log("Teaching in better way");
}

1 Answer

Steven Parker
Steven Parker
243,656 Points

One advantage is memory conservation. In the first case, every instance of the object will have it's own copy of the function. In the second, all instances will share the same function.

Also, with the prototype, you can redefine the function later and all instances will use the new one.