12

I'm trying to decide how to set up my functions in the prototype for my main library.

Should I use:

Library.prototype.funcA = function () {.....};
Library.prototype.fucnB = function () {.....};
etc..

or

Library.prototype = {
    funcA: function () {.....},
    funcB: function () {.....},
    etc..
};

So basically the first choice adds all my functions to the prototype. The second option replaces the prototype with an object containing all my functions. Does it matter?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
ciso
  • 2,887
  • 6
  • 33
  • 58
  • `var foo = new Library(); Library.prototype = {bar: function(){ alert(1); }}; foo.bar();` – Felix Kling Mar 10 '14 at 18:12
  • 2
    If you go with the second approach, don't forget to add `contructor` back: `Library.prototype = {.... , constructor: Library};`. It's not necessary, but it would be strange if it wasn't correctly set. – Felix Kling Mar 10 '14 at 18:17
  • …and of [Defining a Javascript prototype](https://stackoverflow.com/questions/17474390/defining-a-javascript-prototype/17475113#17475113) – Bergi Mar 10 '14 at 18:50

4 Answers4

4

I would go with the first option.

You don't want to completely replace the prototype, as you never know what has been added from another project.

If it's something completely self-contained that only you are working on, the second is an ok option. But it is still not a good habit to get into so you don't inadvertently blow away some functionality something else is counting on.

krillgar
  • 12,596
  • 6
  • 50
  • 86
  • 1
    Strangely I feel this is the best answer from all the given answers, especially the prototype overwriting part. –  Mar 10 '14 at 18:11
  • After reading all the answers and references to other posts, I agree this is the best answer. No point in breaking the prototype chain or deleting everything from the prototype just to add my functions. I can see either one works, but I think keeping the original prototype and adding to it is more elegant. Thank you. – ciso Mar 10 '14 at 18:29
3

In this case, no, it doesn't matter. It's your object, and you're not attempting to inherit from anything, so overwriting the prototype (as opposed to appending to it) doesn't matter.

In the general case, yes, it might matter a lot. You're clobbering whatever existing prototypal methods were available to the object. You shouldn't do that unless you're very sure that your code owns the object in question. Conversely, appending methods to the prototype requires thought as well; other objects may share a prototype with the object whose prototype you're modifying.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • 1
    The OP already knows that `.prototype =` overwrites the existing prototype. See question. – Chris Mar 10 '14 at 18:15
2

It does matter. You have to note that the prototype is an object. So your statement "The second option replaces the prototype with an object containing all my functions." is false. It just reset the prototype object.

So using :

Library.prototype = {
    funcA: function () {.....},
    funcB: function () {.....},
    etc..
};

Is faster, but you delete every prototype function you had before that assignment while :

Library.prototype.funcA

is adding a properties.

So, if you have to add a property (not erase one) use :

Library.prototype.funcA

Else, assign an object.

Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
0

If Library has prototypes properties/methods that you don't want to lose you would want to go with adding them via augmenting the already present prototype. Otherwise it's really up to personal preference. I like the second method because it looks cleaner, but I have used both in my code.

edhedges
  • 2,722
  • 2
  • 28
  • 61