函數(shù)表達式可以 "自調用"。
自調用表達式會自動調用。
如果表達式后面緊跟 () ,則會自動調用。
不能自調用聲明的函數(shù)。
通過添加括號,來說明它是一個函數(shù)表達式:
(function () { var x = "Hello!!"; // 我將調用自己 })();
實例:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鳥教程(runoob.com)</title> </head> <body> <p>函數(shù)可以自動調用:</p> <p id="demo"></p> <script> (function () { document.getElementById("demo").innerHTML = "Hello! 我是自己調用的"; })(); </script> </body> </html>
結果:
函數(shù)可以自動調用:
Hello! 我是自己調用的
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號