Function calling in Javascript with double brackets

后端 未结 5 541
小蘑菇
小蘑菇 2021-02-06 13:23

When I call function hi()() with double brackets the function displays hi output and it will also give error saying, that hi is not functi

5条回答
  •  猫巷女王i
    2021-02-06 13:37

    Putting () after something that evaluates to a function will call that function. So, hi() calls the function hi. Assuming hi returns a function then hi()() will call that function. Example:

    function hi(){
        return function(){return "hello there";};
    }
    
    var returnedFunc = hi();  // so returnedFunc equals function(){return "hello there";};
    var msg = hi()();         // so msg now has a value of "hello there"
    

    If hi() doesn't return a function, then hi()() will produce an error, similar to having typed something like "not a function"(); or 1232();.

提交回复
热议问题