They are not equivalent, especially when considering prototype inheritance.
FooType = function() { this.hello = function() { alert("hello"); }; }; var foo = new FooType(); FooType.prototype.bye = function() { alert('bye!'); }; foo.bye();
The only way you could achieve this in fooFactory is to add it to the object prototype, which is a very bad idea.
The first method is much more significant, in my opinion (since the object has a type that you can check) and can offer much better performance if the prototype is executed correctly. In the first example, every time you create a new FooType object, it creates a new hello function. If you have a lot of such objects, this is a lot of lost memory.
Consider instead:
function FooType() { } FooType.prototype.hello = function() { alert('Hello'); };
source share