This content originally appeared on DEV Community and was authored by JPStupfel
Stumbled upon the following predicament:
function demoChain(name) {
const part1 = ""hi"";
return function () {
const part2 = ""there"";
return function () {
console.log(`${part1.toUpperCase()} ${part2} ${name}`);
};
};
}
demoChain(""Dr. Stephen Strange"")()();
Why, in the above example, would you need to include the
"()()"
when invoking the function
demoChain(""Dr. Stephen Strange"")()();
??????
The answer:
Because demoChain()returns a function, the portion of the above code demoChain(""Dr. Stephen Strange"") is equal to an anonymous function. Therefore, just as writing demoChain in the console wouldn't actually run our code, writing demoChain(""Dr. Stephen Strange"") doesn't actually run our code. You have add the additional () after demoChain(""Dr. Stephen Strange"") in order to execute the function that demoChain(""Dr. Stephen Strange"") is equal to.
Likewise, demoChain(""Dr. Stephen Strange"")() actually equals a function, so we need to place a second pair of () after it to run.
This content originally appeared on DEV Community and was authored by JPStupfel
JPStupfel | Sciencx (2022-04-06T21:28:55+00:00) Getting Things Started. Retrieved from https://www.scien.cx/2022/04/06/getting-things-started/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.