cross-plateforme namespace closure #2
avec des méthodes en chaine
HTML
<strong>Exemple dans node.js en return direct, sans console.log :</strong>
<pre>
$ node
> var bms = require('./bms')
<span style="color:silver">undefined</span>
> bms.mult(5).iple(10)
<span style="color:silver">50</span>
> bms.mult(5)
{ func: <span style="color:cyan">[Function]</span>,
value: <span style="color:lime">'otherfunc !'</span>,
otherfunc: <span style="color:cyan">[Function]</span>,
mult: <span style="color:cyan">[Function]</span>,
iple: <span style="color:cyan">[Function]</span>,
a: <span style="color:silver">5</span>,
b: <span style="color:silver">10</span> }
> bms.iple(10)
<span style="color:silver">50</span>
>
</pre>
CSS
/* sources :
* http://www.richardrodger.com/2013/09/27/how-to-make-simple-node-js-modules-work-in-the-browser/#.VOdF53aBTDo
* et
* http://addyosmani.com/blog/essential-js-namespacing/
*/
/* le ; au début des fonctions permet de mieux gérer la concaténation ??? */
/* Le console.log est inutile pour nodejs, sinon ça affiche un 'undefined' en plus.
le 'return' suffit !!! */
JavaScript
;(function ($, undefined) {
$.func = function () {
console.log("func !");
};
$.value = "otherfunc !";
})(typeof exports === 'undefined' ? this.bms = this.bms || {} : exports);
;(function ($, undefined) {
$.otherfunc = function () {
console.log(this.value);
};
})(typeof exports === 'undefined' ? this.bms = this.bms || {} : exports);
;(function ($, undefined) {
$.mult = function (a) {
this.a = a;
return this;
};
$.iple = function (b) {
this.b = b;
this.c = this.a * this.b;
console.log(this.c);
};
})(typeof exports === 'undefined' ? this.bms = this.bms || {} : exports);
var func = bms.func;
//bms.func(); // func !
func();
bms.otherfunc(); // otherfunc !
bms.mult(3).iple(5); // 15