Skip to content

Instantly share code, notes, and snippets.

@moudy
Created January 21, 2011 19:01
Show Gist options
  • Save moudy/790204 to your computer and use it in GitHub Desktop.
Save moudy/790204 to your computer and use it in GitHub Desktop.
A design pattern to help structure and namespace your Javascript code.
// single global namespace
var Resturaunt = (function (window, document, $, undefined) {
// private variables
var name = 'Building on Bond',
location = (function () {
// nested private variables
var address = { street: '112 Bond St',
city: 'Brooklyn',
state: 'NY',
zip_code: '11217'},
full_address = function () {
return address.street + ' ' + address.city + ' ' + address.state + ' ' + address.zip_code;
};
// end nested private variables
// reveal result of full_address method as address
// making it available within the global Resturaunt object
return { address: full_address() };
}()),
description = function () {
return name + ' is located at ' + location.address;
};
// end private variables
// reveal any neccesary public methods or attributes
// rename if needed and prepend () to make function auto-execute
return { info: description() };
}(this, document, this.jQuery));
// proof it works :)
console.log(Resturaunt.info);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment