Max—— 前端攻城狮 's Blog

A Simple pure blog generated by jekyll

立即执行的函数表达式

<< Back

立即执行的函数表达式(IIFE)

IIFE, Immediately Invoked Function Expression, is a common pattern for creating local scopes

立即执行的函数表达式(IIFE)是一种常见的创建自己作用域范围的代码方式。

(function(){ // the function expression is surrounded by parenthesis
    // variables defined here
    // can't be accessed outside
})(); // the function is immediately invoked

	

一般我们写,习惯在之前添加一个分号;已便于(保证)JS语法解释器合理的解析我们写的代码,而不会抛出异常。

;(function(){ // the function expression is surrounded by parenthesis
    // variables defined here
    // can't be accessed outside
})(); // the function is immediately invoked

    

发表于: 02 Aug 2015