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. django 设置静态文件,static 链接

    这篇文章讲的django 静态static 文件设置,还可以,供参考 http://blog.csdn.net/sinat_21302587/article/details/74059078

  2. 设计模式 - 单件模式(singleton pattern) 具体解释

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/28595349 单件模式(singleton ...

  3. ajax实时获取下拉数据

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ajax ...

  4. 微信公众号验证TOKEN

    服务端验证微信token header('Content-type:text'); define("TOKEN", "weixin"); $signature ...

  5. g2o求解BA 第10章

    1.g2o_bal_class.h1.1 projection.hg2o还是用图模型和边,顶点就是相机和路标,边就是观测,就是像素坐标.只不过这里的相机是由旋转(3个参数,轴角形式,就是theta*n ...

  6. Codeforces Round #461 (Div. 2) B C D

    题目链接:http://codeforces.com/contest/922 B. Magic Forest time limit per test 1 second memory limit per ...

  7. html5--1.10绝对路径和相对路径

    html5--1.10绝对路径和相对路径 学习要点: 绝对路径和相对路径 1.绝对路径 需要指出链接资源的绝对位置,与你的HTML文档的位置无关: 1. 服务器中的位置:href="http ...

  8. 有时候,Visual C++代码中加中文注释会导致错误!

    今天做题发现了个奇葩错误,以此警戒自己. 张某,做的一道题. --------------------------------------------------------------------- ...

  9. UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)

    py文件直接在cmd窗口用python命令执行时正常:代码逐句在ipython中也正常:但是, 在wingIDE中运行报错“UnicodeEncodeError: 'ascii' codec can' ...

  10. 如何在asterisk中限制呼叫路数

      在asterisk中,对于呼叫个数是可以通过call-limit进行限制的.限制办法是通过修改asterisk.conf中maxcalls参数,设置允许的最大呼叫数.这里的最大呼叫数是包括所有的呼 ...