SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot XandroZ's Avatar
    Join Date
    May 2006
    Posts
    147
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript Nested Functions

    I have this code:
    Code:
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>
    <script language="javascript" >
    function outer(data)
    {
    var operand1 = data;
    function inner(operand2)
    {
    alert(operand1 + operand2)
    }
    }
    
    </script>
    <body>
    //<input type="button" value="1" onclick="outer(3)(2)" />
    //<input type="button" value="2" onclick="outer(3)(2)" />
    <div id="target"></div>
    </body>
    </html>
    How do I call the inner function and pass operand?

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    The inner function is called the same way as any other function but since it is defined locally to the outer function it can only be called from within outer and cannot be called from anywhere else since once outer finished the inner function will cease to exist.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    functions in javascript are values, "function foo() {...}" is just a shortcut form of "var foo = function() {...}"


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •