Last Updated: February 25, 2016
·
845
· jamesflorentino

Defining an AMD module in JavaScript

Testing out the protip feature

Here's how I define a RequireJS/NodeJS compliant module:

Verbose

define('ninja', function(require, exports, module) {

    var Class = require('./class-util');

    var Ninja = {
        teleport: function() {},
        shuriken: function() {}
        katana: function() {}
    };

    return module.exports = Class.extend(Ninja);

});

Minimal

define('ninja', function(require, exports, module) {

    var Class = require('./class-util');

    return module.exports = Class.extend({
        teleport: function() {},
        shuriken: function() {}
        katana: function() {}
    });

});

More minimal

define('ninja', function(require, exports, module) {

    return module.exports = require('./class-util').extend({
        teleport: function() {},
        shuriken: function() {}
        katana: function() {}
    });

});