Factory/Constructor
In JavasScript, functions are defacto constructors. When called with new, the this will refer to the newly created instance. However, when called as a function, this will refer to the scope from which it was called (a namespace object or possibly window).
So here's a way to make either approach return an instance:
var MyClass = function (cf) {
if (!(this instanceof arguments.callee)) {
return new arguments.callee(cf);
}
/* Constructor stuff here */
};
Using this type of function body will allow you to treat the function as an instance constructor
var foo = new MyClass(blah);
or a factory method
var foo = MyClass(blah);