I've been trying to make my javascript code better by creating a namespace for my code so that there are no global variable/function clashes and also putting everything in the namespace in an anonymous function so everything is private unless explicitly expressed otherwise through the return statement.
Atm, I'm getting a "Uncaught Type Error: object is not a function"
var Namespace = (function() {
//private variables/functions
var a;
var b;
function priv_func1() {
//do stuff
}
//public variables/functions
return {
pub_var1: b,
pub_func1: function() {
//do stuff
}
};
})();
$(document).ready(function() {
var myName = new Namespace();
myName.pub_func1();
}
So when I remove the () at the end of the Namespace definition which turns the function declaration into a function expression, I get no error, but in the examples I've seen they have the () in there, so I'm wondering what's going on.
I also put this at the start of the Namespace definition as well, to correct when the user accidentally omits the new keyword.
if (!(this instanceof Namespace))
return new Namespace();
EDIT: Also, should I put my document ready function before or after the namespace definition.