我最近正在学习JavaScript和jQuery,但我明白了,jQuery有很多方法可以通过“ $ .function(arg)/jQuery.function(arg)”调用...所以我想只是为了学习而类似的东西,这是我的测试代码...

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script type="text/javascript">
    (function (){
        function hello_world(){
            document.write("Hello planet!");
        }
    })();
</script>
</head>
<body>

<script type="text/javascript">
    /*  HOW DO I CALL "HELLO WORLD IN HERE?"  */
</script>

</body>
</html>

最佳答案

您可以将IIFE分配给变量,但是必须从IIFE返回hello_world函数:

var hello_world = (function () {
    return function hello_world(){
        console.log("Hello planet!");
    }
})();

hello_world(); // Hello planet!


DEMO

关于javascript - 如何在JavaScript中的自调用函数内调用方法?有点像jQuery,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32396945/

10-12 12:39
查看更多