(function (window, document, undefined) {})(window, document)什么意思?
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)什么意思?的更多相关文章
- (译)(function (window, document, undefined) {})(window, document); 真正的意思
由于非常感兴趣, 我查询了很多关于IIFE (immediately-invoked function expression)的东西, 如下: (function (window, document, ...
- JS (function (window, document, undefined) {})(window, document)的真正含义
原文地址:What (function (window, document, undefined) {})(window, document); really means 按原文翻译 在这篇文章中,我 ...
- javascript匿名函数自执行 (function(window,document,undefined){})(window,document);
使用匿名自执行函数的作用: (function(window,document,undefined){})(window,document); 1.首先匿名函数 (function(){}) (); ...
- 详解jquery插件中(function ( $, window, document, undefined )的作用。
1.(function(window,undefined){})(window); Q:(function(window,undefined){})(window);中为什么要将window和unde ...
- js实现跨域(jsonp, iframe+window.name, iframe+window.domain, iframe+window.postMessage)
一.浏览器同源策略 首先我们需要了解一下浏览器的同源策略,关于同源策略可以仔细看看知乎上的一个解释.传送门 总之:同协议,domain(或ip),同端口视为同一个域,一个域内的脚本仅仅具有本域内的权限 ...
- window.showModalDialog与window.open()使用
window.showModalDialog 有些浏览器不兼容,尝试用window.open() 封装替代,需要打开子窗口后向父窗口传递数据. <html> <script src= ...
- 详解jquery插件中;(function ( $, window, document, undefined )的作用
在jquery插件中我们经常看到以下这段代码 1 2 3 ;(function ( $, window, document, undefined ){ //函数体内具体代码 })(jQuery, wi ...
- jquery插件中(function ( $, window, document, undefined )的作用
在jquery插件中我们经常看到以下这段代码 ;(function ( $, window, document, undefined ){ //函数体内具体代码 })(jQuery, window,d ...
- ;(function($,window,document,undefined){})(jQuery,window,document)
;(function($,window,document,undefined){})(jQuery,window,doucment) 1.自调函数(function(){})() 2.好处是不会产生任 ...
随机推荐
- 打开蓝牙debug hci log
Android4.2之前抓取hci log都是通过hcidump命令完成的,但是Android4.2 Bluetooth引入了Bluedroid,这是一个新的蓝牙协议栈.所以抓取hci log的方法也 ...
- 关于button的自动刷新
今天在开发中遇到了ajax传数据到后台,处理结果正常,返回的resultMap是一个Map<String,Object>类型,但是返回时显示'Fail to load response d ...
- 爬虫前戏(回顾掌握) -- HTTP和HTTPS
一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...
- Spring Boot2.0之Admin-UI分布式微服务监控中心
前面https://www.cnblogs.com/toov5/p/9823353.html 说的很不好用哈哈 还需要json格式化 我们可以用Admin-UI 比较爽歪歪 原理: 将所有服务的监控 ...
- javascript(5)
(1)数组的细节: 基本用法 var 数组名=[元素值,元素值...]; 元素的值可以是任意类型. 数组是引用类型. js里的引用. 在函数参数列表中,如果传入的是基本类型,那 按值传递.如果传入的是 ...
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...
- hdu1085Holding Bin-Laden Captive!组合问题
题目连接 题目意思:有单位价值为1 2 5的三种硬币,分别给出他们的数量,求用这些硬币不能组成的最小的价值 解题思路:普通的母函数 普通的母函数: 利用母函数的思想可以解决很多组合问题,下面举例说明: ...
- VC++动态链接库(DLL)编程深入浅出(转帖:基础班)
1.概论 先来阐述一下DLL(Dynamic Linkable Library)的概念,你可以简单的把DLL看成一种仓库,它提供给你一些可以直接拿来用的变量.函数或类.在仓库的发展史上经历了“无库-静 ...
- leetcode 104 Maximum Depth of Binary Tree(DFS)
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 网络编程学习笔记-listen函数
listen函数使用主动连接套接口变为被连接套接口,使得一个进程可以接受其它进程的请求,从而成为一个服务器进程.在TCP服务器编程中listen函数把进程变为一个服务器,并指定相应的套接字变为被动连接 ...