1.IIFE(即时调用的函数表达式),它采取以下表达式:

(function (window, document, undefined) {
//
})(window, document);

JavaScript具有函数作用域,因此可以创建一些需要的“私有范围”。

“IIFE”之所以被创造出来是因为它们是直接调用的函数表达式。

这意味着它们在运行时被立即调用,我们也不能再调用它们了,它们只运行一次。

(function (window) {

})(window);

(window); 是调用函数的地方,我们通过window对象,然后这个函数被传递到函数中,我也把它命名为window。

你可以认为这是毫无意义的,因为我们应该给它命名不同的东西,但是现在我们也将使用window。

我们还能把所有的东西都传过去:

(function (window, document) {
// 我们通常需要 window 和 document
})(window, document);

2.那么undefined是啥??

在ECMAScript 3中,undefined是可变的,这意味着它的值可以被重新赋值,比如undefined = true;

幸运的是,在 ECMAScript 5 中的 ('use strict';)语法将会抛出一个错误。

于是我们可以通过传入undefined来保护自己的 IIFE,也就是说如果有人来给undefined赋值了,也不会有问题:

undefined = true;
(function (window, document, undefined) {
// undefined 是一个局部未定义的变量
})(window, document);

缩小局部变量是IIFE模式的神奇之处,传入局部变量名可以随意的命名:

(function (window, document, undefined) {
console.log(window); // Object window
})(window, document);
// 这两个功能是一样的
(function (a, b, c) {
console.log(a); // Object window
})(window, document);

也可以将jquery引进来:

(function ($, window, document, undefined) {
// use $ to refer to jQuery
// $(document).addClass('test');
})(jQuery, window, document); (function (a, b, c, d) {
// becomes
// a(c).addClass('test');
})(jQuery, window, document);

这也意味着你不需要调用jQuery.noConflict();或者其他任何东西来替代$。

备注:

What (function (window, document, undefined) {})(window, document); really means

(function (window, document, undefined) {})(window, document)什么意思?的更多相关文章

  1. (译)(function (window, document, undefined) {})(window, document); 真正的意思

    由于非常感兴趣, 我查询了很多关于IIFE (immediately-invoked function expression)的东西, 如下: (function (window, document, ...

  2. JS (function (window, document, undefined) {})(window, document)的真正含义

    原文地址:What (function (window, document, undefined) {})(window, document); really means 按原文翻译 在这篇文章中,我 ...

  3. javascript匿名函数自执行 (function(window,document,undefined){})(window,document);

    使用匿名自执行函数的作用: (function(window,document,undefined){})(window,document); 1.首先匿名函数 (function(){}) (); ...

  4. 详解jquery插件中(function ( $, window, document, undefined )的作用。

    1.(function(window,undefined){})(window); Q:(function(window,undefined){})(window);中为什么要将window和unde ...

  5. js实现跨域(jsonp, iframe+window.name, iframe+window.domain, iframe+window.postMessage)

    一.浏览器同源策略 首先我们需要了解一下浏览器的同源策略,关于同源策略可以仔细看看知乎上的一个解释.传送门 总之:同协议,domain(或ip),同端口视为同一个域,一个域内的脚本仅仅具有本域内的权限 ...

  6. window.showModalDialog与window.open()使用

    window.showModalDialog 有些浏览器不兼容,尝试用window.open() 封装替代,需要打开子窗口后向父窗口传递数据. <html> <script src= ...

  7. 详解jquery插件中;(function ( $, window, document, undefined )的作用

    在jquery插件中我们经常看到以下这段代码 1 2 3 ;(function ( $, window, document, undefined ){ //函数体内具体代码 })(jQuery, wi ...

  8. jquery插件中(function ( $, window, document, undefined )的作用

    在jquery插件中我们经常看到以下这段代码 ;(function ( $, window, document, undefined ){ //函数体内具体代码 })(jQuery, window,d ...

  9. ;(function($,window,document,undefined){})(jQuery,window,document)

    ;(function($,window,document,undefined){})(jQuery,window,doucment) 1.自调函数(function(){})() 2.好处是不会产生任 ...

随机推荐

  1. 相比ICO,DAICO主要有这两方面优势

    都说ICO已死,很有一部分人对无币区块链持保留态度,自从V神提出DAICO一来,大家似乎看到了新的方向,不少项目围绕其展开.那对比ICO,DAICO有哪些优势呢?主要是以下两点: DAICO维护了投资 ...

  2. Intellij Idea生成JavaDoc

    JavaDoc是一种将注释生成HTML文档的技术,生成的HTML文档类似于Java的API,易读且清晰明了.在简略介绍JavaDoc写法之后,再看一下在Intellij Idea 中如何将代码中的注释 ...

  3. log4net 初步使用

    自从知道了log4net之后,就一直使用的它,一直没有问题,最近由于项目变动,便将一部分的代码分离出来,然后咋UI项目中调用loghelper,便发现在本地测试一切正常,可是发布到服务器之后便不正常了 ...

  4. Mac OS访问Windows共享文件夹

    原文地址:http://blog.csdn.net/jinhill/article/details/7246922 最近开始研究Mac OS,遇到的第一个问题就是如何在Mac OS中访问Windows ...

  5. <再看TCP/IP第一卷>关于链路层的知识细节及相关协议

    在TCP/IP协议族中,链路层的主要有三个目的: (1)为IP模块发送和接受数据报 (2)为ARP模块发送ARP请求和接受ARP应答 (3)为RARP发送RARP请求和接受RARP应答 TCP/IP支 ...

  6. 应用程序无法启动(0*c000007b)

    2个插件就解决  一个是DX缺失工具检查那个 一个是运行库缺失检查

  7. 我所理解的RESTful Web API [设计篇]【转】

    原文:http://www.cnblogs.com/artech/p/restful-web-api-02.html <我所理解的RESTful Web API [Web标准篇]>Web服 ...

  8. 分享知识-快乐自己:论Hibernate中的缓存机制

    Hibernate缓存 缓存: 是计算机领域的概念,它介于应用程序和永久性数据存储源之间. 缓存: 一般人的理解是在内存中的一块空间,可以将二级缓存配置到硬盘.用白话来说,就是一个存储数据的容器.我们 ...

  9. centos7搭建redis主从复制,并模拟故障切换。

    Cntos7搭建redis主从复制,并模拟故障主从切换 主从复制搭建 主机:192.168.161.179 从机:192.168.161.180 1.        安装主redis 自己本地环境,关 ...

  10. AtCoder AGC #2 Virtual Participation

    在知乎上听zzx大佬说AGC练智商...于是试了一下 A.Range Product 给$a$,$b$,求$\prod^{b}_{i=a}i$是正数,负数还是$0$ ...不写了 B.Box and ...