我最近在学习 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!
关于javascript - 如何在 JavaScript 的自调用函数中调用方法?有点像 Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32396945/