Skip to content

Instantly share code, notes, and snippets.

@vdurmont
Created September 22, 2014 03:57
Show Gist options
  • Save vdurmont/518197dbe0714053e0ca to your computer and use it in GitHub Desktop.
Save vdurmont/518197dbe0714053e0ca to your computer and use it in GitHub Desktop.
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else {
root.Hello = factory();
}
}(this, function() {
return function() {
var done = false;
return {
print: function(name) {
if (!name) {
name = "World";
}
console.log("Hello "+name+"!");
done = true;
},
hasPrint: function() {
return done;
}
}
}
}));
var Hello = function() {
var done = false;
return {
print: function(name) {
if (!name) {
name = "World";
}
console.log("Hello "+name+"!");
done = true;
},
hasPrint: function() {
return done;
}
}
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AMD example</title>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.14/require.min.js"></script>
<script type="text/javascript">
requirejs.config({
"paths": {
"Hi": "./hello-amd"
}
});
require(['Hi'], function(Hi) {
var h = new Hi();
console.log(h.hasPrint());
h.print();
console.log(h.hasPrint());
h.print("Vincent");
});
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Basic example</title>
<script type="text/javascript" src="hello.js"></script>
<script type="text/javascript">
var h = new Hello();
console.log(h.hasPrint());
h.print();
console.log(h.hasPrint());
h.print("Vincent");
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment