JavaScript立即调用函数表达式(IIFE)
JavaScript立即调用函数表达式(IIFE)是一个JavaScript函数,它在定义后立即执行,因此无需手动调用IIFE。
以下是JavaScript中立即调用函数表达式(IIFE)的代码实例−
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { font-size: 18px; font-weight: 500; } </style> </head> <body> <h1>JavaScript Immediately Invoked Function Expressions (IIFE)</h1> <div class="sample"></div> <script> let sampleEle = document.querySelector(".sample"); (function () { sampleEle.innerHTML ="This code is invoked immediately as soon as it is defined"; })(); </script> </body> </html>
输出
广告