factories versus libraries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Poulos

    factories versus libraries

    If I have a "factory" that is in this form:

    function ma() {
    blah;
    }
    ma.prototype.a = function() {
    blah2;
    }
    var obj = new ma()

    and I have a "library" that's of this form:

    ma = {
    a: function() {
    blah2;
    },
    b: blah;,
    }

    Is there any advantage using one over the other? I've done a google and
    have found out some things about both but I don't have enough
    programming theory to make a decision.


    Andrew Poulos
  • Martin Honnen

    #2
    Re: factories versus libraries



    Andrew Poulos wrote:
    [color=blue]
    > If I have a "factory" that is in this form:
    >
    > function ma() {
    > blah;
    > }
    > ma.prototype.a = function() {
    > blah2;
    > }
    > var obj = new ma()
    >
    > and I have a "library" that's of this form:
    >
    > ma = {
    > a: function() {
    > blah2;
    > },
    > b: blah;,
    > }
    >
    > Is there any advantage using one over the other?[/color]

    With the first version you can instantiate as much objects having the
    same method a as needed e.g.
    var ma1 = new ma();
    var ma2 = new ma();
    The second version creates exactly one object having a method a. If you
    only want one object of that "type" then the second version suffices but
    if you want to created several objects of a "type" then the first
    version is what you need.


    --

    Martin Honnen

    Comment

    • Douglas Crockford

      #3
      Re: factories versus libraries

      > If I have a "factory" that is in this form:[color=blue]
      >
      > function ma() {
      > blah;
      > }
      > ma.prototype.a = function() {
      > blah2;
      > }
      > var obj = new ma();[/color]

      What you have there is a constructor.
      See http://www.crockford.com/javascript/inheritance.html
      [color=blue]
      > and I have a "library" that's of this form:
      >
      > ma = {
      > a: function() {
      > blah2;
      > },
      > b: blah;,
      > }[/color]

      What you have there is an object made using the object literal notation.
      See http://www.crockford.com/javascript/survey.html and http://www.JSON.org
      [color=blue]
      > Is there any advantage using one over the other? I've done a google and
      > have found out some things about both but I don't have enough
      > programming theory to make a decision.[/color]

      Which to use depends on how many objects you are going to be producing.
      If you need a lot of them, it makes sense to have a constructor. If you
      need only one, it makes sense to make a single object.

      Comment

      Working...