factories versus libraries

A

Andrew Poulos

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
 
M

Martin Honnen

Andrew said:
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?

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.
 
D

Douglas Crockford

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

What you have there is a constructor.
See http://www.crockford.com/javascript/inheritance.html
and I have a "library" that's of this form:

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

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
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top