JS modules chaining
basic module pattern
by toubia95
HTML
<p id="result"></p>
JavaScript
var res = document.getElementById("result");
(function( $, undefined ) {
$.Dec = function (x) {
this.dec = x;
return this;
};
$.Mult = function (y) {
this.mult = y;
this.decmult = this.dec * y;
return this;
};
}( window.Fonc = window.Fonc || {}));
(function( $, undefined ) {
$.Display = function () {
if (this.decmult) {
return this.decmult;
} else if (this.dec) {
return this.dec;
} else if (this.mult) {
return this.mult;
} else {
return 0;
}
};
}( window.Fonc = window.Fonc || {}));
mult3 = Fonc.Dec(3).Mult(9).Display();
res.insertAdjacentHTML('beforeend', 'result: '+ mult3 + '<br>');