Skip to content

Instantly share code, notes, and snippets.

@pmachapman
Last active August 29, 2015 14:19
Show Gist options
  • Save pmachapman/57393813e18b8c7a8ce7 to your computer and use it in GitHub Desktop.
Save pmachapman/57393813e18b8c7a8ce7 to your computer and use it in GitHub Desktop.
Modular Javascript Example
// The object will be called test
var test = (function () {
// Use strict mode for JavaScript parsing
// The browser uses this
"use strict";
// Global Variables - JSLint stuff...
/*global $*/
/*global utilities*/
var myPublicVariable;
var myPrivateVariable;
/**
* It is my function
*
* @param x The parameter!
* @param y The second parameter!
* @returns The result!
*/
function myFunction(x, y) {
myPrivateVariable = x + y;
return myPrivateVariable;
}
// Standard jquery can be inside the function
$(document).ready(function () {
});
// Expose only the functions and variables we want to expose
return {
myFunction: myFunction,
myPublicVariable: myPublicVariable
};
})();
// Call the function
test.myFunction(1, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment