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

Asked

Viewed 88 times

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 that at the event onfocus that input call the job one_level_2 so that only the instructions internal to it were executed.

What is the correct way to call this function?

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

  • This example you gave is not very clear to me. Why do you auto-execute the function right after you declare it? these functions use/change variables of the most external scope?

3 answers

1

I think that’s 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()"/>

0

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

In order to perform this call, you need to extract the function out even.

Because the form that the function was declared, is only executed, cannot 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();"/>

0

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

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 for 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.