JS for Node and Browser

by toubia95

HTML

<b>Console du Browser :</b>
<pre>
    toto("hello");
    <span class="ok">hello</span>
    bms.tata("hi");
    <span class="ok">hi</span>
</pre>
<b>Terminal :</b>
<pre>
    $ node
    > var bms = require('./bms');
    <span class="comment">undefined</span>
    > bms.tata("ça marche ?");
    <span class="ok">ça marche ?</span>
    <span class="comment">undefined</span>
    > toto("Hello !");
    <span class="error">ReferenceError: toto is not defined</span>
</pre>

<p>Voir <a href="http://jsfiddle.net/toubia95/wae6m1jd/" target="_blank">ici</a> et <a href="http://jsfiddle.net/toubia95/p6s8035g/" target="_blank">là</a> pour le chainage des modules standards.</p>

CSS

/* source : http://caolanmcmahon.com/posts/writing_for_node_and_the_browser/ */
.comment {color:silver;}
.error {background:red; color:white;padding : 0 5px;}
.ok {background:green;color:white;padding : 0 5px;}

JavaScript

// Browser
function toto(string) {
    console.log(string);
}

// Browser & Node
(function(exports){
   exports.tata = function(string){
        console.log(string);
    };
})(typeof exports === 'undefined'? this['bms']={}: exports);


toto("hello"); //Browser console hello
bms.tata("hi"); //Browser console hi