Skip to content

Instantly share code, notes, and snippets.

@markdaws
Created February 7, 2012 05:18

Revisions

  1. @invalid-email-address Anonymous revised this gist Feb 7, 2012. 1 changed file with 36 additions and 10 deletions.
    46 changes: 36 additions & 10 deletions common.js
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,40 @@
    function(exports) {
    // common.js ======================================

    if(!exports) {
    // This is client side code
    (function(exports) {

    window.MyApp = window.MyApp = window.MyApp || {};
    exports = window.MyApp.common = window.MyApp.common || {}
    }

    exports.foo = function() {
    return 'bar';
    // Define all your functions on the exports object
    exports.foo = function() {
    return 'bar';
    };

    })((typeof process === 'undefined') || !process.versions ? null : exports);
    })((typeof process === 'undefined' || !process.versions)
    ? window.common = window.common || {}
    : exports);

    // ================================================


    // clientfile.html ==================================
    // Using the common code in a browser
    <html>
    <head>

    <script src="common.js"></script>
    </head>
    <body>

    <script>
    alert(window.common.foo());
    </script>
    </body>
    </html>

    // ================================================

    // nodefile.js ====================================
    // Using the code from a node.js file

    var common = require('./common');
    console.log(common.foo());

    // ================================================
  2. @invalid-email-address Anonymous created this gist Feb 7, 2012.
    14 changes: 14 additions & 0 deletions common.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    function(exports) {

    if(!exports) {
    // This is client side code

    window.MyApp = window.MyApp = window.MyApp || {};
    exports = window.MyApp.common = window.MyApp.common || {}
    }

    exports.foo = function() {
    return 'bar';
    };

    })((typeof process === 'undefined') || !process.versions ? null : exports);