Select to view content in your preferred language

What happens when another function is inside require([ ],function( ));

797
2
Jump to solution
10-29-2013 10:54 AM
LeiZhou
Deactivated User
In AMD style codes, I don't understand what happens when a function is in inside the require([...], function() {}); for example, the following codes:
require([...], function() { .....  function ABC () {a = c + b; } ..... });


The function ABC is very confusing to me.  When the program runs, when will function ABC be executed?  To understand this, should I go study more dojo language? Thanks a lot!
0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Esri Frequent Contributor
In AMD style codes, I don't understand what happens when a function is in inside the require([...], function() {}); for example, the following codes:
require([...], function() { .....  function ABC () {a = c + b; } ..... });


The function ABC is very confusing to me.  When the program runs, when will function ABC be executed?  To understand this, should I go study more dojo language? Thanks a lot!


The ABC function lives in the scope of the require() function, but isn't executed unless you call it in the require function. It is only accessible inside that particular require function. This isn't really a Dojo specific issue. Modules simply scope functionality. But when you define() a module, unless you return something, it doesn't provide anything. This is why you may come across errors like variable equals 3 or something odd, because maybe you forgot to place a return in the module.

To help illustrate, let's look at a define() module.

// ABCModule define([], function() {   function ABC (b, c) {     var a = c + b;     return a;   }    return ABC; // defined modules should return something, object or function. });  // Main JS file require(['./ABCModule'], function(ABC) {   ABC(1, 2); // returns 3; });


If you didn't actually return ABC in the defined module, you would never be able to use it, as it's not exposed to the outside world.
AMD modules essentially use the revealing module pattern to share their innards (or the important parts anyway) with the everyone else.
http://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript
http://requirejs.org/docs/whyamd.html#amd

Here are a couple of links that discuss scope.
http://dailyjs.com/2012/07/23/js101-scope/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope
http://javascriptplayground.com/blog/2012/04/javascript-variable-scope-this/

Also, you typically only have a single require() per application, an entry point to start the application. This SO answer covers if pretty nicely.
http://stackoverflow.com/questions/11559270/what-is-the-main-difference-between-require-and-define-f...

Hope that helps a bit.

View solution in original post

0 Kudos
2 Replies
ReneRubalcava
Esri Frequent Contributor
In AMD style codes, I don't understand what happens when a function is in inside the require([...], function() {}); for example, the following codes:
require([...], function() { .....  function ABC () {a = c + b; } ..... });


The function ABC is very confusing to me.  When the program runs, when will function ABC be executed?  To understand this, should I go study more dojo language? Thanks a lot!


The ABC function lives in the scope of the require() function, but isn't executed unless you call it in the require function. It is only accessible inside that particular require function. This isn't really a Dojo specific issue. Modules simply scope functionality. But when you define() a module, unless you return something, it doesn't provide anything. This is why you may come across errors like variable equals 3 or something odd, because maybe you forgot to place a return in the module.

To help illustrate, let's look at a define() module.

// ABCModule define([], function() {   function ABC (b, c) {     var a = c + b;     return a;   }    return ABC; // defined modules should return something, object or function. });  // Main JS file require(['./ABCModule'], function(ABC) {   ABC(1, 2); // returns 3; });


If you didn't actually return ABC in the defined module, you would never be able to use it, as it's not exposed to the outside world.
AMD modules essentially use the revealing module pattern to share their innards (or the important parts anyway) with the everyone else.
http://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript
http://requirejs.org/docs/whyamd.html#amd

Here are a couple of links that discuss scope.
http://dailyjs.com/2012/07/23/js101-scope/
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope
http://javascriptplayground.com/blog/2012/04/javascript-variable-scope-this/

Also, you typically only have a single require() per application, an entry point to start the application. This SO answer covers if pretty nicely.
http://stackoverflow.com/questions/11559270/what-is-the-main-difference-between-require-and-define-f...

Hope that helps a bit.
0 Kudos
LeiZhou
Deactivated User
Thanks a lot! It is a very useful information.
0 Kudos