By using an anonymous self-executing function, you can allow for public and private attributes/methods.
This is the pattern I like the most:
(function ($, MyObject, undefined) {
MyObject.publicFunction = function () {
console.log("Public function");
};
var privateFunction = function () {
console.log("Private function");
};
var privateNumber = 0;
MyObject.sayStuff = function () {
this.publicFunction();
privateFunction();
privateNumber++;
console.log(privateNumber);
};
// You can even nest the namespaces
MyObject.nestedNamespace = MyObject.nestedNamespace || {};
MyObject.nestedNamespace.logNestedMessage = function () {
console.log("Nested namespace function");
};
}(jQuery, window.MyObject = window.MyObject || {}));
MyObject.sayStuff();
MyObject.sayStuff();
MyObject.nestedNamespace.logNestedMessage();
MyObject.publicFunction();
Learned about it from the comments here and this article.