Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I parse the value of a nested function to another nested function to get the value in JavaScript

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading