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. js实现随机选取[10,100)中的10个整数,存入一个数组,并排序。 另考虑(10,100]和[10,100]两种情况。

    1.js实现随机选取[10,100)中的10个整数,存入一个数组,并排序. <!DOCTYPE html> <html lang="en"> <hea ...

  2. Redis3.x HA 方案(基于 Sentinel 方式)

    第一部分 Redis-HA 搭建 一.Redis-HA 拓扑 一主两从,主从复制,故障时主从切换 三个Redis节点 + Sentinel 节点 Master          127.0.0.1   ...

  3. fragment 动态加载

    /** * 测试使用Fragment(动态使用) 1. * 使用FragmentManager和FragmentTransaction动态使用一个Fragment 2. 方式: * add(viewI ...

  4. Machine Learning No.2: Linear Regression with Multiple Variables

    1. notation: n = number of features x(i) = input (features) of ith training example  = value of feat ...

  5. C++ 结构体多元素sort排序调用时的写法

    //总结一下,结构体数据排序的快速写法 //以后在遇到需要写的时候,不要迟疑快速写完 struct node { int u, v, w; }a[10000]; //假设该结构体有3个元素 //现在仅 ...

  6. SPOJ - PHRASES Relevant Phrases of Annihilation —— 后缀数组 出现于所有字符串中两次且不重叠的最长公共子串

    题目链接:https://vjudge.net/problem/SPOJ-PHRASES PHRASES - Relevant Phrases of Annihilation no tags  You ...

  7. 【html学习整理】meta,img,表格,表单

    meta标签: 作用: 给搜索引擎用 . 告诉浏览器是什么编码 <meta charset="UTF-8"> <meta name="keywords& ...

  8. Linux学习之路(三)搜索命令

    1.文件搜索命令locate 2.命令搜索命令whereis与which 3.字符串搜索命令grep 4.find命令与grep命令的区别 locate命令相对于find命令搜索非常快,find命令在 ...

  9. listen 73

    Give Time to Feel Less Time-Squeeze Meetings, calls, kids, dogs, errands, exercise—and all those ema ...

  10. 《java编程思想》读后笔记:一,标签

    标签 是后面跟有冒号的标识符,格式如下: label : java中通过break与continue关键词可以完成类似于跳转的操作,其实现机制便是标签. 虽然很少有人使用,但是其有自身的适用场景:多层 ...