Solved

javascript code - function in function

Posted on 2014-03-17
3
356 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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks so much

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

744 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