anaconda_wly
anaconda_wly

Reputation: 159

dojo require function's parameter

Take simple examples as below:

require(["dojo/_base/ready", "dojo/_base/declare"], function(ready, declare) {
}

How to explain ready and declare to dojo core? Class name? In another example I often see:

require(["js/somemodule.js"], function(someName) {
});

Many times I see someName not the same as somemodule(ready, declare are the same as the module name at least), nor was it any identifier I could found in somemodule.js or its base. What's the matter? I guess when the argument in require function should be declared in some where and hold a value.

Upvotes: 0

Views: 411

Answers (1)

Dimitri Mestdagh
Dimitri Mestdagh

Reputation: 44755

The variable name someName is something you choose yourself. It is in fact the variable referencing to the module. So that means that someName refers to the module js/somemodule.js.

If you want to call a function of js/somemodule.js, you use:

someName.myFunction();

Usually people give it the same name as the module (or something similar) because it's easier to remember. But it's not always the case, because if there are dashes in the module name, people usually use camel case, for example:

require(["dojo/dom-construct"], function(domContruct) {
    domConstruct.create(...);
});

But the following is also correct (and does exactly the same):

require(["dojo/dom-construct"], function(theAwesomeModule) {
    theAwesomeModule.create(...);
});

Not only Dojo uses this, but the AMD loader is a general principle in JavaScript. For example, look at the AMD info page of Require.js.

Upvotes: 4

Related Questions