What is the correct way to call function inside another nested function?

0

For example:

function principal()
{    function one_level_1(){...}
     one_level_1();
     function two_level_1(){...}
     two_level_1();
     function three_level_1()
     {    function one_level_2(){...}
          one_level_2();
     }
     three_level_1();
}

<input type="text" onfocus="one_level_2();"/>

I would like the event onfocus of this input to call the function one_level_2 so that only the internal statements to it were executed.

What is the correct way to call this function?

I tried <input type="text" onfocus="(principal().three_level_1().one_level_2();)"/> , but without success.

    
asked by anonymous 25.04.2017 / 20:54

3 answers

1

I think this is what you need.

function principal() {

  function one_level_1(){
    console.log('one level 1');
  }
  
  principal.one_level_1 = one_level_1;
    
  function two_level_1(){
    console.log('two level 1');
  }
  
  principal.two_level_1 = two_level_1;
     
  function three_level_1(){    
    function one_level_2(){      
      console.log('three_level_1 -> one level 2');
    }
            
    three_level_1.one_level_2 = one_level_2;
  }
  
  principal.three_level_1 = three_level_1;
}

principal();
principal.three_level_1();
<input type="text" onfocus="principal.three_level_1.one_level_2()"/>
    
25.04.2017 / 22:16
0

The functions declared in this way are not possible to be called by onfocus , only by the principal function.

To make this call, you need to extract the function out.

Because the way the function was declared, it only runs, can not form a hierarchy

function principal()
{    function one_level_1(){...}
     one_level_1();
     function two_level_1(){...}
     two_level_1();
     function three_level_1()
     {    
          one_level_2();
     }
     three_level_1();
}
function one_level_2(){...}

<input type="text" onfocus="one_level_2();"/>
    
25.04.2017 / 20:58
0

You just need to get declarations of functions from within the scope of the principal function, by doing so, they will be accessible globally and not only in the scope of the principal function.

function principal() {
  one_level_1();

  two_level_1();

  three_level_1();
}

function one_level_1() { ... }

function two_level_1() { ... }

function three_level_1() {
  one_level_2();
}

function one_level_2() { ... }

<input type = "text" onfocus = "one_level_2();" / >

See below a functional example:

function principal() {
  console.log('Função principal');
  nivel1();

  function escopoPrincipal() {
    console.log('Função acessível apenas no escopo principal');
  }
  escopoPrincipal();
}

function nivel1() {
  console.log('Função nível 1');
  nivel1_2();
}

function nivel1_2() {
  console.log('Função nível 2');
}
<a href="javascript:void(0)" onclick="console.clear();principal()">Função principal</a><br />
<a href="javascript:void(0)" onclick="console.clear();nivel1()">Função nível 1</a><br />
<a href="javascript:void(0)" onclick="console.clear();nivel1_2()">Função nível 1 2</a><br />
<a href="javascript:void(0)" onclick="console.clear();escopoPrincipal()">Função do escopo da função principal - não acessível globalmente</a>
    
25.04.2017 / 21:10