(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.好处是不会产生任 ...
随机推荐
- 移动App该怎样保存用户password
版权声明:本文为横云断岭原创文章,未经博主同意不得转载.微信公众号:横云断岭的专栏 https://blog.csdn.net/hengyunabc/article/details/34623957 ...
- PHP中的排序函数sort、asort、rsort、krsort、ksort区别分析(转)
sort() 函数用于对数组单元从低到高进行排序. rsort() 函数用于对数组单元从高到低进行排序. asort() 函数用于对数组单元从低到高进行排序并保持索引关系. arsort() 函数用于 ...
- 使用 Docker LNMP 部署 PHP 运行环境
简介 Docker LNMP 是基于 Docker 的 PHP 集成开发环境. Github 地址:https://github.com/YanlongMa/docker-lnmp 包含软件 ngin ...
- mysql忘记root密码或报错:ERROR 1044 (42000): Access denied for user ”@’localhost’ to database ‘xx‘
有的时候忘记了root密码或其他用户的密码,登录的时候报错:ERROR 1044 (42000): Access denied for user ”@’localhost’ to database ' ...
- Git core objects
Git core objects Core objects in git blob object tree object commit object Git low level commands gi ...
- Linux-3.14.12内存管理笔记【kmalloc与kfree实现】【转】
本文转载自:http://blog.chinaunix.net/uid-26859697-id-5573776.html kmalloc()是基于slab/slob/slub分配分配算法上实现的,不少 ...
- Oracle 数据库SQL
原作者:http://blog.csdn.net/jihuanliang/article/details/7205968 总体说说可能出现的原因: 情况场景: 表A中有个字段是外键,关联了表B中的某字 ...
- 分享知识-快乐自己:2017IDEA破解教程
首先 修改host文件: 文件路径:C:\Windows\System32\drivers\etc\hosts 修改:将“0.0.0.0 account.jetbrains.com”追加到hosts文 ...
- css中字体大小在不同浏览器兼容性问题
css中使用font-size设定字体大小,不同浏览器的字体height一样,但是width不同,比如在火狐和谷歌中,font-size:20px,字体的高度变为20px,但是谷歌的字体宽度比火狐长 ...
- Java_正则_00_资源贴
二.参考资料 1.揭开正则表达式的神秘面纱