Reputation: 27
See this:
var funkyFunction = function() {
return function() {
return "FUNKY!"
}
}
var theFunk = funkyFunction(funkyFunction())
I need theFunk variable to be assigned value "FUNKY!" from the inner function but I have no idea how to go about it?
Upvotes: 1
Views: 190
Reputation: 3
With ES6/ES7 You can return a function from within a function without naming the inner function.
const myOuterFunction = () => {
// the ...args just places all arguments into an array called args (can be named whatever)
console.log("OuterGuy");
return (...args) => {
//Whatever Else you want the inner function to do
console.log(args[0]);
};
};
//The way you call this
const innerGuy = myOuterFunction();
const randomVariableThatINeed = 'Yay!';
//Call inner guy
innerGuy(randomVariableThatINeed);
Upvotes: 0
Reputation: 5118
Since funkyFunction
is returning a function, the result of invoking funkyFunction
can then be invoked:
var func = funkyFunction(); // 'func' is the inner function
var theFunk = func(); // 'theFunc' = 'FUNKY!'
Upvotes: 3