Thursday, August 16, 2012

javaScript: Nested Functions

Another Intresting concept that poped up in JavaScript
:nested functions inside of other functions : This provides a way to make a function private. If you want these be accessable there must be a accessor method in the outfunction otherwise they will be unreachable outside of the outter function

Here is an Example of what this looks like.

<!DOCTYPE html>
<html>
<head>
    <title>Nested Functions</title>
    <script type="text/javascript">
          function initiateMadness() {
            var sparta = {
                name : "Sparta"
            };
            function madness() {
                alert("THIS. IS. " + this.name.toUpperCase() + ".");
            }
            document.onclick = makeAMessenger(madness, sparta);
        }
       
//access the content of the inner function
   function makeAMessenger(func, context)
        {  
            window.name = context.name;
            func();
        }
        initiateMadness();
    </script>
</head>
<body>
<script type="text/javascript">
</script>
<h1> Heading 1 </h1>
<p> THis is my program </p>
    
</body>
</html>

No comments:

Post a Comment