I'm part of a small study group at work that's trying to get a better grasp on what makes JavaScript tick. In our recent discussions about objects, we've learned that an object's public methods are recreated each time an object is instantiated, while methods assigned to the object's prototype are only created once and inherited by all instances. From what I understand, both public methods and those assigned to the prototype are publicly accessible.
The question I have, then, is why bother creating public methods at all if adding to the prototype is apparently more efficient? What benefit does the public method provide that the prototype doesn't?
var name = new Student()
and I added a prototype to name it would be accessible tovar name2 = new Student()
as well – Heindrickthis.GetName = function() { ... }
inside the constructor ofStudent
, you can access that from both too, however it's "recreated each time". – Insistence