Search code examples
javascriptrequirejsjs-amd

Asynchronous Module Definition: difference between beta.verb() and require("beta").verb()


I was studying Asynchronous Module Definition from wiki.commonjs.org and I am wondering what is the meaning of this piece of code.
In particular, my question is:
return beta.verb(); and return require("beta").verb(); have the same effect.
If not what is the difference?

define("alpha", ["require", "exports", "beta"], function (require, exports, beta) {
    exports.verb = function() {
        return beta.verb();
        //Or:
        return require("beta").verb();
    }
});

Solution

  • beta is already initialized to the value of require("beta") before the callback function is called. From the definition of define on that page:

    The dependencies [argument] must be resolved prior to execution of the module factory function, and the resolved values should be passed as arguments to the factory function with argument positions corresponding to index in the dependencies array