Skip to content

Instantly share code, notes, and snippets.

@awsp
Forked from markdaws/common.js
Last active August 29, 2015 14:20
Show Gist options
  • Save awsp/bc566bd82a971549afdb to your computer and use it in GitHub Desktop.
Save awsp/bc566bd82a971549afdb to your computer and use it in GitHub Desktop.
// common.js ======================================
(function(exports) {
// Define all your functions on the exports object
exports.foo = function() {
return 'bar';
};
})((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());
// ================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment