JavaScript Nested Functions

Discussion in 'JavaScript' started by XandroZ, Jan 30, 2007.

  1. #1
    I have this 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>
    
    Code (markup):
    How do I call the inner function and pass operand?
     
    XandroZ, Jan 30, 2007 IP
  2. datropics

    datropics Peon

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #2
    I do apologise if I don't understand but you seem to be mixing a few things up here. If you want to call the function inner then define inner as a non-nested function OR call outer and have outer call inner.

    Also in your onClick event handler you have outer(3)(2). I don't believe you can do that. It should be outer(3,2). You will possibly need to redefine outer to accept two parameters.

    I may be totally off base but that is how I 'THINK' you want it to work
     
    datropics, Jan 30, 2007 IP
  3. pbmods

    pbmods Guest

    Messages:
    18
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I agree with datropics. If you want to create two functions, you should implement them separately.

    However, if you wanted to create a function whose code was dependent on some variable, for example, use this syntax:

    var f_someFunction = function(s_parameterOne, obj_parameterTwo ...) { /* Function code... or an eval statement goes here. */ };
    Code (markup):
     
    pbmods, Feb 5, 2007 IP