skip to Main Content

I’m in this situation where I have to create a function inside a function, let’s call the parent function exo() and the nested one childexo().I need to parse the value of a result from childexo() to another function outside both exo() and childexo(), let’s call that one, as caller. The note here should be that, I need to get the value of the result of childexo() and not the function when I test it with console.log.

function exo(){
  // SOME CALCULATIONS

  return childexo = function() {
    var Itext;
    Itext.textContent = "Phew, you just killed your bug";
    console.log(Itext);
  }
}

exo();

function newFunc(childexo) {
  // SOME CALCULATIONS

  var xImagine = function() {
    console.log(childexo);
  }
}

newFunc(childexo); 

I noticed childexo can’t be put like this childexo() as the newFunc parameter.

I just need the console.log of childexo() to be displayed instead of displaying the function childexo() in console like outterHTML.

And uh, I’m new to JavaScript, so I may only understand clear codes.

2

Answers


  1. I answered about the same way when this was asked (and then closed by the OP) yesterday.

    The function exo returns another function. It needn’t be named inside exo since the caller can assign it to a variable.

    Another function, newFunc, can certainly take a function as param. You can "mention" a function just by using the variable name to which it is assigned. You can "invoke" that function by placing an argument list after it in parens ().

    Here’s the OP code restated (with errors removed) in ES6 style…

    const exo = () => {
      //SOME CALCULATIONS
      return () => {
        var Itext = "Phew, you just killed your bug";
        console.log(Itext);
      }
    }
    
    const newFunc = childexo => {
      //SOME CALCULATIONS
      // here we "invoke" the passed function. this can be done inside
      // yet another function as the OP attempts, but ultimately we must invoke
      childexo();
    }
    
    // this is what @Pointy meant by "saving" the returned function
    let fnFromExo = exo();  
    newFunc(fnFromExo);
    Login or Signup to reply.
  2. what you want is ….

    1. exo() will return subfunction
      so you need to save somewhere (‘asdf’)
    function exo(){
      //SOME CALCULATIONS
      return function(){
        var Itext;
        Itext.textContent = "Phew, you just killed your bug";
        console.log(Itext);
      }
    }
    var asdf = exo();
    

    do not assign something at return.

    it is unneccessary

    1. newFunc will execute given function
      so you need to call function.
    function newFunc(exoArgs){
      //SOME CALCULATIONS
      var xImagine = function(){
        console.log(exoArgs());
      }
    }
    newFunc(asdf); 
    

    but childexo is return void, so console.log of newFunc will print ”
    you will get

    Phew, you just killed your bug
       // empty line
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search