Solved

javascript code - function in function

Posted on 2014-03-17
3
364 Views
Last Modified: 2014-03-19
i am trying to understand this code-
in this code, a function is written inside a function -

   <script>
 
      require([
        "esri/map", "esri/layers/ArcGISTiledMapServiceLayer", 
        "esri/domUtils", "esri/request",
        "dojo/parser", "dojo/number", "dojo/json", "dojo/dom", 
        "dijit/registry", "modules/RasterLayer",

        "dijit/layout/BorderContainer", "dijit/layout/ContentPane", 
        "dijit/form/HorizontalSlider", "dojo/domReady!"
      ], function(
        Map, ArcGISTiledMapServiceLayer, 
        domUtils, esriRequest,
        parser, number, JSON, dom, 
        registry, RasterLayer
      ) {

        //some code 
        function mapLoaded() {
//code of maploaded
        }

        function getRasterData() {
       //code 
        }

        function setElevation() {
        //code 
         }
 
         function supports_canvas() {
                //code 
         }
      });
    </script>

Open in new window


Why the functions "setElevation()", "supports_canvas()", "getRasterData() ",  "mapLoaded()"  
are written within

 "function(
        Map, ArcGISTiledMapServiceLayer,
        domUtils, esriRequest,
        parser, number, JSON, dom,
        registry, RasterLayer
      ) "

 thanks so much

pl guide
0
Comment
Question by:ts84zs
3 Comments
 
LVL 23

Accepted Solution

by:
Michael74 earned 300 total points
ID: 39936069
It is called closure.

Have a look at these links for more info
http://web.archive.org/web/20120101221030/http://blog.morrisjohns.com/javascript_closures_for_dummies.html
http://javascriptissexy.com/understand-javascript-closures-with-ease/

A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.

The inner function has access not only to the outer function’s variables, but also to the outer function’s parameters. Note that the inner function cannot call the outer function’s arguments object, however, even though it can call the outer function’s parameters directly.
0
 
LVL 28

Assisted Solution

by:Pravin Asar
Pravin Asar earned 200 total points
ID: 39937416
Having a the function inside a function, is a style some developers used to build functions which work similar to methods in purely objected-oriented code.

This style of code is good for building custom objects.
(see http://www.w3schools.com/js/js_obj_intro.asp)

JavaScript is a Object-oriented language. If you want to learn more about this ,
please visit

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript
0
 

Author Comment

by:ts84zs
ID: 39937572
thanks so much

actually i do not understand the whole code ,,,  I will open another question to explain the whole code....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now