2

In parent:

function outer(){
    function inner(){
        alert("hello");
    }
}

in iframe

parent.outer.inner();

does not work.

Edit: So they are private. No problem. The reason for doing this is to try and hide these functions from global scope as much as possible. I thought putting them behind a single function name with a pretty unique name would be the best way to do this.

Essentially do I need to

parent:

function outer(action){

    if(action == "inner")
       inner()

    function inner(){
        alert("hello");
    }
}

iframe: parent.outer("inner");

Is there a more elegant way of doing this?

SystemicPlural
  • 5,629
  • 9
  • 49
  • 74
  • 1
    If you want the inner() function to be callable from outside the outer() function, you have to make a reference to it trough closure. – Šime Vidas Nov 25 '10 at 13:11

4 Answers4

2

Nested functions are private and you can at best only specify arguments for inner functions outside the outer function.

Explanation can be found here https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope under "Nested functions and closures"

Mantar
  • 2,710
  • 5
  • 23
  • 30
0

By using closure :

   function outer(){
        function inner(){
        alert("I am Inner");
        }
        return inner ;
   }
   var x = outer();
   x();

If you want to use variable of inner function than we can do in this way :

  function outer(){
        function inner(){
         var y=10;  return y;    
       }
       return inner ;
 }
 var x = outer();
 x();
Anshul
  • 9,312
  • 11
  • 57
  • 74
0

This seems to answer my edit fairly well How to turn a String into a javascript function call?

Community
  • 1
  • 1
SystemicPlural
  • 5,629
  • 9
  • 49
  • 74
-1

One approach is using an object

function Outer(){
    this.inner = function(){
        alert("hello");
    }
}

var o = new Outer()
o.inner();
Frank Forte
  • 2,031
  • 20
  • 19