Last Updated: February 25, 2016
·
1.2K
· 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() {}
    });

});

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside