I am trying to create an object that gets returned without the new keyword in javascript?
My code structure so far;
myLib.func = (function() {
"use strict";
function func() {
this._init();
};
func.prototype._init = function() {
this.someVar = 5;
};
Return func;
})();
This obviously only works when using the new keyword;
new myLib.func();
How can I make it so that I can just do;
var func = myLib.func();
But it would still return an object that is exactly the same as the first example?
What I have tried
myLib.func = (function() {
"use strict";
function func() {
if (window === this) {
return new myLib.func();
} else {
this._init();
}
};
func.prototype._init = function() {
this.someVar = 5;
};
Return func;
})();
This does not work I learned from an example on slide 25 of John Resig's tips on building a library, http://ejohn.org/blog/building-a-javascript-library/
I know there are already existing frameworks, but rolling my own will make me learn alot, and as you can see that isn't alot at the moment!