Can’t do exactly that, can something close. The function signature cb
does not predict that it has parameters, so you cannot pass a function that has this parameter.
One solution is to change funcao()
why cb()
accept parameter and also accept a parameter to be passed. I think it is not what you want, what you want is not possible.
function funcao1(cb, message) {
return cb(message);
}
function funcao2(message) {
console.log(message);
}
funcao1(funcao2, "ok");
I put in the Github for future reference.
Another possibility is to use an enclosure, so the message goes along:
function funcao1(cb) {
return cb();
}
var message = "ok";
function funcao2() {
console.log(message);
}
funcao1(funcao2);
I put in the Github for future reference.
That’s not exactly a closure (is a global, enclosing if you were inside a function), but passes the idea of how it works, can put the funcao2()
and message
inside another function that is there the way I am talking, even because it must be so that it will actually be in real code.
It should not be what you want yet, but it is the alternatives. You can have new alternatives understanding the real problem, maybe not even need all this. The artificial example is not bad to explain the basics, but does not give room for other alternatives.
Still have user140828 option in comment below:
function funcao1(cb) {
return cb();
}
function funcao2(message) {
console.log(message);
}
funcao1(() => funcao2("ok"));
I put in the Github for future reference.
You create a indirect extra, but I still see that doing so may not make much sense. I think you need to take a closer look at the real problem.
I would also suggest using
funcao1(() => funcao2('ok'))
, but I did not understand very well what the author of the question wants.– Andre
@user140828 good, put the credit to you.
– Maniero