Skip to content

Instantly share code, notes, and snippets.

@branquito
Created December 18, 2014 01:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save branquito/fa7feddc63ecb15cb4bf to your computer and use it in GitHub Desktop.
Save branquito/fa7feddc63ecb15cb4bf to your computer and use it in GitHub Desktop.
module-pattern-example.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Module Pattern Example</title>
</head>
<body>
<script src="module-pattern-example.js"></script>
</body>
</html>
/*
* module-pattern-example.js
* Copyright (C) 2014 branchito <branchito@mail.sonikete.com>
*
* Distributed under terms of the MIT license.
*/
var MyModule = (function(w, undefined){
// Store in this object, private variables and methods across multiple intantiations
var _privates = {};
// Start module name with capital letter, unlike "ordinary" functions
// This is our constructor function
function MyModule(){
/**
* `this` refers to the instance of `MyModule` when created
*/
this.method_one = function method_one(){
alert("method_one");
};
this.method_two = function method_two(){
alert("method_two");
};
}
// Expose access to the constructor
// w.MyModule = MyModule;
return MyModule;
})(window);
// Examples
var myModule = new MyModule();
myModule.method_one();
myModule.method_two();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment