So from making a server side emulator for a mmorpg in node.js I have learnt some ways to make modules.
Here I will be sharing some of the ways to write them
<script type="syntaxhighlighter" class="brush: js"><![CDATA[
(function() {
// Put any requires your module needs here
var YourObject = function(func) { // Has example of passing in a callback,
// Any private values you could put here
var something = 1;
// Here is where you can put public propertys and what not
this.Something = 1; // public propertys here
if (func) this.func = func; // Overrides the prototype func
}
// You can also put public/protected functions here using prototype
YourObject.prototype = {
func: function() { console.log('Not yet implemented'); } // Used if func not passed in as paramater :)
getID: function() { return this.ID; },
getName: function() { return this.Name; },
};
module.exports = YourObject;
})();
//To make it work on web client and node.js server
var obj = new YourObject();
//Or you could use reference to your object rather than an instance of it depending if you want to create more of it or only have 1
if(typeof module !== "undefined" && module.exports) {
module.exports = obj;
}
if(typeof window !== "undefined") {
window.YourObject = obj;
}
]]></script>
Simple eh. In node to have private functions I guess you could put them in the scope outside your object.
But in web browsers this may pollute the global scope.
http://ejohn.org/blog/simple-javascript-inheritance/