如何在javascript中返回匿名函数

dra*_*sea 0 javascript

<html>
    <head>
        <script>
             function test(){                   
                 return function(){
                  alert("hi");
                }                
             }
             test();
        </script>
    </head>
    <body>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

这是我的代码,我可以问为什么它不能正常工作?

Dim*_*rov 11

因为你正在返回你的函数而不是调用它.试试这个:

test()();
Run Code Online (Sandbox Code Playgroud)

这是一个小提琴