Last active
February 11, 2024 23:13
-
-
Save remarkablemark/17b2f358aec0f0ea79167d831319a7bd to your computer and use it in GitHub Desktop.
Example of creating an AMD module using `define()`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define(function() { | |
/** | |
* Add one or more numbers. | |
* | |
* @param {...number} | |
* @return {Number} - The sum or 0. | |
*/ | |
function add() { | |
var sum = 0; | |
var num; | |
for (var i = 0, len = arguments.length; i < len; i++) { | |
num = arguments[i]; | |
if (typeof num === 'number' && !isNaN(num)) { | |
sum += num; | |
} | |
} | |
return sum; | |
} | |
return add; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.2/require.min.js"></script> | |
<script> | |
requirejs(['https://rawgit.com/remarkablemark/17b2f358aec0f0ea79167d831319a7bd/raw/4cbaea52d38ad25661a6c584ea40e4f3ac0ef389/add.amd.js'], function(add) { | |
console.assert(add(1, 2), 3); | |
console.log('2 + 2 = ', add(2, 2)); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment