Function calling in Javascript with double brackets

后端 未结 5 486
小蘑菇
小蘑菇 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条回答
  •  旧巷少年郎
    2021-02-06 13:37

    The return value of this function is a string which is not a callable object.

    function hi()
    {
        document.write("hi");
        return "hello"; // <-- returned value
    }
    

    But if you want to call this function multiple times you can use a for-loop or some things else.

    Example of hi()():

    function hi(){
        return function(){ // this anonymous function is a closure for hi function
           alert('some things')
        }
    }
    

    JS Fiddle: here

    If you want to call hello function immediately after hi try this:

     function hi()
        {
            document.write("hi");
            return hello; //<-- no quote needed
            // In this context hello is function object not a string
        }
    

提交回复
热议问题