Code Conventions - PMeschenmoser/Visual-Analyzer-for-News-Dependencies GitHub Wiki
Our aim is to produce OOP-like Javascript code: well-structured, reusable, modularised and encapsulated. Especially for our frontend modules, we define the following code pattern:
var Classname = function(container,options){ //container: CSS selector
var public = this;
var _classvar = []; //private
function _constructor(){
//...further initialization steps....
$(container).data('public', public); //make module accessible
}
function _privateMethod(){
var methodvar = 123;
}
public.bar = function(){
}
_constructor();
}
Initialization is simply done via new Classname('.foo',{})
.
We are binding public methods to the DOM container- calling a public method is possible via $('.foo').data('public').bar();
. With this pattern, we achieve simple module communication without the need for global variables.