Created
December 18, 2014 01:58
-
-
Save branquito/fa7feddc63ecb15cb4bf to your computer and use it in GitHub Desktop.
module-pattern-example.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<title>Module Pattern Example</title> | |
</head> | |
<body> | |
<script src="module-pattern-example.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* module-pattern-example.js | |
* Copyright (C) 2014 branchito <[email protected]> | |
* | |
* 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