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
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 insideexo
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…
what you want is ….
so you need to save somewhere (‘asdf’)
do not assign something at return.
it is unneccessary
so you need to call function.
but childexo is return void, so console.log of newFunc will print ”
you will get