JavaScript的IIFE(即时执行方法)
(1)标准写法
- (function (window, document, undefined) {
- //
- })(window, document);
(2)作用域Scope
JavaScript有function作用域,所以function首先创建一个私有的作用域,在执行的时候都会创建一个执行上下文。
- (function (window, document, undefined) {
- var name = '张三';
- })(window, document);
- console.log(name); // 因为作用域不同,这里的name未定义。
调用方法一:
- var logMyName = function (name) {
- console.log(name);
- };
- logMyName('李四');
调用方法二:
- var logMyName = (function (name) {
- console.log(name);
- })('王五');
需要注意的是需要用括号把函数内容括起来:
- function () {
- // ...
- })();
没有括号的话会报语法错:
- function () {
- // ...
- }();
也可以强制JavaScript识别代码(一般很少这么用):
- !function () {
- // ...
- }();
- +function () {
- // ...
- }();
- -function () {
- // ...
- }();
- ~function () {
- // ...
- }();
比如:
- var a = !function () {
- return true;
- }();
- console.log(a); // 打印输出 false
- var b = (function () {
- return true;
- })();
- console.log(b); // 打印输出 true
(3)参数Arguments
- 介绍IIFE
- IIFE的性能
- 使用IIFE的好处
- IIFE最佳实践
- jQuery优化
在Bootstrap源码(具体请看《Bootstrap源码解析》)和其他jQuery插件经常看到如下的写法:
- +function ($) {
- }(window.jQuery);
这种写法称为:
IIFE (Imdiately Invoked Function Expression 立即执行的函数表达式)。
一步步来分析这段代码。
先弄清函数表达式(function expression)和 函数声明(function declaration)的区别:
函数表达式 Function Expression - var test = function() {};
函数申明 Function Declaration - function test() {};
函数表达式中的函数可以为匿名函数,也可以有函数名,但是该函数实际上不能直接使用,只能通过表达式左边的变量 a 来调用。
- var a = function(){
- alert('Function expression');
- }
- var b = new a();
函数声明时必须有函数名。
- function a(){
- alert('Function declaration');
- }
- a();
这是一个匿名函数。
- function () {
- }
你也许注意到匿名函数在console下会报错。console的执行和报错如下:
function(){}
通过一元操作符+变成了函数表达式。也可以使用 - ~ !等其他一元运算符或者括号,目的是为了引导解析器,指明运算符附近是一个表达式。以下是三种经典方式 :
- +function () {
- };
- (function () {
- });
- void function() {
- };
函数表达式通过 末尾的() 来调用并运行。就是一个IIFE。
- +function () {
- }();
- (funtion () {
- })();
代码性能
运算符:+加-减!逻辑非~位取反,返回NaN(Not A Number)。
“()”组运算符:返回表达式的执行结果undefined。
void:按运算符结合语句执行,返回 undefined。
这几种的性能对比结果:

可见+性能最差(在Firefox下差距更明显),其他几种都差不多。不过IIFE只执行一遍,对js执行效率的影响特别小,使用哪种还是看个人爱好。
传参,为了避免$与其他库或者模板申明冲突,window.jQuery 作为参数传递。
- +function (x) {
- console.log(x);
- }(3);
- +function ($) {
- }(window.jQuery);
使用IIFE的好处
总结有4点:提升性能、有利于压缩、避免冲突、依赖加载
1、减少作用域查找。使用IIFE的一个微小的性能优势是通过匿名函数的参数传递常用全局对象window、document、jQuery,在作用域内引用这些全局对象。JavaScript解释器首先在作用域内查找属性,然后一直沿着链向上查找,直到全局范围。将全局对象放在IIFE作用域内提升js解释器的查找速度和性能。
传递全局对象到IIFE的一段代码示例:
- // Anonymous function that has three arguments
- function(window, document, $) {
- // You can now reference the window, document, and jQuery objects in a local scope
- }(window, document, window.jQuery); // The global window, document, and jQuery objects are passed into the anonymous function
2、有利于压缩。另一个微小的优势是有利于代码压缩。既然通过参数传递了这些全局对象,压缩的时候可以将这些全局对象匿名为一个字符的变量名(只要这个字符没有被其他变量使用过)。如果上面的代码压缩后会变成这样:
- // Anonymous function that has three arguments
- function(w, d, $) {
- // You can now reference the window, document, and jQuery objects in a local scope
- }(window, document, window.jQuery); // The global window, document, and jQuery objects are passed into the anonymous function
3、避免全局命名冲突。当使用jQuery的时候,全局的window.jQuery对象 作为一个参数传递给$,在匿名函数内部你再也不需要担心$和其他库或者模板申明冲突。 正如James padolsey所说:
An IIFE protects a module’s scope from the environment in which it is placed.
4、通过传参的方式,可以灵活的加载第三方插件。(当然使用模块化加载更好,这里不考虑。)举个例子,如果a页面需要使用KindEditor,a.html引入kindeditor.js 和 a.js
你可能会这么写 a.js:
- $(function() {
- var editor
- KindEditor.ready(function(K) {
- editor = K.create('textarea[data-name="kindeditor"]', {
- resizeType : 1
- })
- })
- })
b页面不需要使用Kindeditor,没有引入kindeditor.js。但是在合并JS代码后,b页面也会执行a.js中的代码,页面报错Uncaught ReferenceError: KindEditor is not defined。也就是b页面执行了KindEditor,难道所有页面都要加载Kindeditor源文件?
可以这么修改a.js,将KindEditor变量参数化,通过给立即执行的函数表示式的参数赋值,那么其他页面都不需要加载Kindeditor源文件
- +function( KindEditor){
- var editor
- if(KindEditor){
- KindEditor.ready(function(K) {
- editor = K.create('textarea[data-name="kindeditor"]', {
- resizeType : 1
- })
- })
- }
- }(KindEditor || undefined)
IIFE最佳实践
反对使用IIFE的其中一个理由是可读性差,如果你有大量的JavaScript代码都在一段IIFE里,要是想查找IIFE传递的实际参数值,必须要滚动到代码最后。幸运的是,你可以使用一个更可读的模式:
- (function (library) {
- // Calls the second IIFE and locally passes in the global jQuery, window, and document objects
- library(window, document, window.jQuery);
- }
- // Locally scoped parameters
- (function (window, document, $) {
- // Library code goes here
- }));
这种IIFE模式清晰的展示了传递了哪些全局对象到你的IIFE中,不需要滚动到长文档的最后。
jQuery优化
一段看上去写法有点像的代码。大部分项目用这段代码做作用域,这段代码会在DOM加载完成时初始化jQuery代码。
- $(function(){
- });
这种写法等同于
- $(document).ready(function(){
- // 在DOM加载完成时初始化jQuery代码。
- });
- $(window).load(function(){
- // 在图片等媒体文件加载完成时,初始化jQuery代码。
- });
结合IIFE的最佳实践,更好的写法是,立即执行document ready
- +function ($) {
- $(function(){
- })
- }(window.jQuery)
最佳实践
- // IIFE - Immediately Invoked Function Expression
- +function(yourcode) {
- // The global jQuery object is passed as a parameter
- yourcode(window.jQuery, window, document);
- }(function($, window, document) {
- // The $ is now locally scoped
- // Listen for the jQuery ready event on the document
- $(function() {
- // The DOM is ready!
- }));
具体请看工程师,请优化你的代码
其他
在Bootstrap和其他插件中经常看到如下写法:
- +function ($) { "use strict";
- }(window.jQuery);
关于字符串"use strict";请看严格模式
传递参数给IIFE
- (function (window) {
- // 这里可以调用到window
- })(window);
- (function (window, document) {
- // 这里可以调用到window和document
- })(window, document);
undefined参数
在ECMAScript 3中undefined是mutable的,这意味着可以给undefined赋值,而在ECMASCript 5的strict模式('use strict';)下是不可以的,解析式时会报语法错。
所以为了保护IIFE的变量需要undefined参数:
- (function (window, document, undefined) {
- // ...
- })(window, document);
即使有人给undefined赋值也没有关系:
- undefined = true;
- (function (window, document, undefined) {
- // undefined指向的还是一个本地的undefined变量
- })(window, document);
(4)代码压缩Minifying
- (function (window, document, undefined) {
- console.log(window); // window对象
- })(window, document);
代码压缩后,undefined的参数不再存在,但是由于 (window, document); 的调用没有传递第三个参数,所有c依然是一个本地undefined变量,所有参数中undefined的名字是无所谓什么的,只需要知道他指向的是一个undefined变量。
- (function (a, b, c) {
- console.log(a); // window对象
- })(window, document);
除undefined变量外,其他的所有希望只在函数内部有效的变量都可以通过参数传递进去,比如以下的jQuery对象。
- (function ($, window, document, undefined) {
- // 使用 $ 指向 jQuery,比如:
- // $(document).addClass('test');
- })(jQuery, window, document);
- (function (a, b, c, d) {
- // 代码会被压缩为:
- // a(c).addClass('test');
- })(jQuery, window, document);
JavaScript的IIFE(即时执行方法)的更多相关文章
- 在WebBrowser中执行javascript脚本的几种方法整理(execScript/InvokeScript/NavigateScript) 附完整源码
[实例简介] 涵盖了几种常用的 webBrowser执行javascript的方法,详见示例截图以及代码 [实例截图] [核心代码] execScript方式: 1 2 3 4 5 6 7 8 9 1 ...
- javascript实现每秒执行一次的方法
javascript实现每秒执行一次的方法 <pre> i=0; function showzhandou() { $('.zhandouresult p').eq(i).fadeIn() ...
- 【JavaScript专题】--- 立即执行函数表达式
一 什么是立即执行函数表达式 立即执行函数表达式,其实也可以叫初始化函数表达式,英文名:IIFE,immediately-inovked-function expression.立即执行函数表达式就是 ...
- javascript中数组常用的方法
在JavaScript中,数组可以使用Array构造函数来创建,或使用[]快速创建,这也是首选的方法.数组是继承自Object的原型,并且他对typeof没有特殊的返回值,他只返回'object'. ...
- [转]Javascript中的自执行函数表达式
[转]Javascript中的自执行函数表达式 本文转载自:http://www.ghugo.com/javascript-auto-run-function/ 以下是正文: Posted on 20 ...
- 深入理解javascript中的立即执行函数(function(){…})()
投稿:junjie 字体:[增加 减小] 类型:转载 时间:2014-06-12 我要评论 这篇文章主要介绍了深入理解javascript中的立即执行函数,立即执行函数也叫立即调用函数,通常它的写法是 ...
- Jquery中$(document).ready()与传统JavaScript中的window.onload方法的区别(2016/8/3)
Jquery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,不过与window.onload方法还是有区别的. 1.执行时间 ...
- ASP.NET CS文件中输出JavaScript脚本的3种方法以及区别
Response.Write 与 Page.ClientScript.RegisterStartupScript 与 Page.ClientScript.RegisterClientScriptB ...
- JavaScript之call()和apply()方法详解
简介:apply()和call()都是属于Function.prototype的一个方法属性,它是JavaScript引擎内在实现的方法,因为属于Function.prototype,所以每个Func ...
随机推荐
- Struts2的输入校验(2)——客户端校验
Struts2的输入校验(2) --客户端校验 Struts2客户端校验的使用: (1)使用Struts2的标签生成输入页面的表单: (2)为该<s:form>元素添加validate=& ...
- Struts2的处理结果(一)——处理结果的配置
Struts2的处理结果(一) --处理结果的配置 1.处理结果 在Action处理完用户请求之后,并不会直接生成响应,而是把一个字符串返回给Struts2框架,再由框架选择此字符串结果对应的物理视图 ...
- C#单击按钮显示图片,带开始停止
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- DataTables warning : Requested unknown parameter '5' from the data source for row 0
在该项目中我使用了jquery.dataTables.js来作为我的前端数据表格. 表格的官网地址:https://www.datatables.net/ 一.jsp部分代码片段如下: <tab ...
- MySQL性能优化的21个最佳实践 和 mysql使用索引
1. 为查询缓存优化查询 当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一个缓存中,这样,后续的相同的查询就不用操作表而直接访问缓存结果了. 2. EXPLAIN SELECT 查询 ...
- hihoCoder太阁最新面经算法竞赛17
比赛链接:http://hihocoder.com/contest/hihointerview26 A.排序后枚举两个点,确定一个矩形后二分剩下两个点. #include <bits/stdc+ ...
- oracle命令识记
连接数据库 sqlplus /nolog; conn / as sysdba; set ORACLE_SID=实例名; 查看表结构命令 select table_name from user_tabl ...
- Hibernate之N+1问题
什么是hibernate的N+1问题?先了解这样一个描述: 多个学生可以对应一个老师,所以student(n)---teacher(1).Stu类中有一个属性teacher.在hibernate配置文 ...
- 常用ADB命令
adb devices 查看当前已与pc端连接的设备序列号 adb install -r +apk名称 安装应用程序(带-r参数表示强制安装,可以覆盖安装) adb un ...
- uploads 上传图片
public static function upFile($r,$p='../images/link/',$type='gif,jpg,png',$named=0){ $newnames = nul ...