zenwaichi
zenwaichi

Reputation: 197

How to get the handle of an anonymous module with requirejs?

define(['somethingRequired'],function(somethingRequired) {
   // code
});

let's say this code is inside test.js , using the console, how could I inspect it and fiddle with its properties after it has been loaded? From the documentation I've seen that modules can be given names but that's not the case, I am wondering what happens to anonymous modules. Sorry in advance if the question is unclear or poorly written, accepting feedbacks

Update for Louis answer: I tried both the short form //var foo = require(['foo']); var foo = require('foo'); and the asynchronous call require(['foo'], function (foo_) { foo = foo_; }); Neither made the module leak in global scope. Am I still missing something? Also tried to add a console.log('done') in the asynchronous call but it never showed up in console

Upvotes: 1

Views: 2084

Answers (2)

Louis
Louis

Reputation: 151401

You've misunderstood what it means to call define without a module name as the first parameter. The only thing it does is let RequireJS figure out the module name at run time on the basis RequireJS's configuration. For instance, let's suppose I have a file which from the baseUrl directory would have the relative path foo/bar.js and this file calls define without setting a module name. I could load this module by doing require(['foo/bar'], ... However, if I have the following configuration:

paths: {
    bar: 'foo/bar'
}

Then I could load it with require(['bar'], ... If I had a define in the file that also sets the module name, either one of two methods of loading the module would certainly fail. If I have define('foo/bar', ... then the first require would work but not the second. If I have define('bar', ... then the second require would work but not the first.

Ultimately, though, when you load a module that does not call define with a module name, you still have to load it using some name. So you need to figure out what name the application you are examining uses to refer to the module that interests you, and use that name.

The only things you can fiddle with once a module is loaded are the values that your module export. Anything not exported is off-limits.

Once you've got a name for your module, you have to issue a require call at the console to get a reference to the module. If the module is already loaded, you can use var foo = require(module_name). This is the synchronous form of require which is convenient to use but works only if a module is already loaded. If the module is not already loaded, then you have to use something like require([module_name], function (foo_) { foo = foo_ }); (note how the first parameter is an array here) to get a global foo to be set to your module. See this answer for a more verbose expalanation.

Upvotes: 1

Ajey
Ajey

Reputation: 8202

You can make use of self invoking functions inside the module so that it is executed as soon as the module is loaded.

More about self invoking functions - A self invoking anonymous function expression

Upvotes: 0

Related Questions