24

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?

6
  • Prototype addition to the object creates the same method for other objects of same class (in this case a function) to be added. for instance if i have var name = new Student() and I added a prototype to name it would be accessible to var name2 = new Student() as well Commented Oct 8, 2015 at 12:19
  • 1
    @AkshayKhandelwal Assuming the OP means doing this.GetName = function() { ... } inside the constructor of Student, you can access that from both too, however it's "recreated each time". Commented Oct 8, 2015 at 12:20
  • 1
    Another similar question: Advantages of using prototype, vs defining methods straight in the constructor? Commented Oct 8, 2015 at 12:28
  • @Pablo Nope. I disagree. This is not a duplicate. It definitely makes sense since the difference that he's pointing out is creation of the public methods again. Commented Oct 8, 2015 at 12:28
  • @AkshayKhandelwal He is pointing that public methods are recreated every time you instance a new object. Last 2 lines of the question are the key here Commented Oct 8, 2015 at 12:31

1 Answer 1

23

Answering this specifically:

What benefit does the public method provide that the prototype doesn't?

A method added within the constructor has access to private information, eg:

function Student() {
    var name = 'Bob';
    this.GetName = function() {
        return name;
    }
}

Student.prototype.SomeOtherPublicMethod = function() {
    //no access to name
}
Sign up to request clarification or add additional context in comments.

10 Comments

This makes sense, @James Thorpe.
Unless you wrap the "class" into a self-executing function and move private methods and fields out of the constructor.
@Kroltan I'd be interested to see how you construct such a thing, where the prototype methods have access to the private variables, but each instance of the object has it's own data. (genuinely, no sarcasm...!)
@AurumPotabile if you want to call them privileged, then no - there's no difference between that and a public method defined in the constructor. What makes them "privileged" is the fact that they can access the innards, not that they do.
@AurumPotabile no, it's just a name that's commonly used to refer to them. You are too much trying to force those keywords onto a language that doesn't have them. The keywords "private" and "public" (and "privileged") were given to you because you are probably familiar with them, but they don't exist in JS. They allow you to get started, but for deeper understanding, think about the core principles of JS such as function scope, closures/iifes, etc. which are the true reasons why the things are the way they are. This is a healthier base for knowledge than keywords of some other language.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.