原文地址:What (function (window, document, undefined) {})(window, document); really means

按原文翻译

在这篇文章中,我们将探讨标题所暗示的内容,并解释自调用函数设置给我们带来了什么。

有趣的是,我被问到关于IIFE(即时调用的函数表达式)的很多,它采用以下设置:

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

那么为什么不写一篇关于它的文章呢? :-)

首先,这是一系列不同的事情。从顶部:

JavaScript具有函数作用域,因此首先创建了一些需要的“私有范围”。例如:

(function (window, document, undefined) {
var name = 'Todd';
})(window, document); console.log(name); //name is not defined,它在一个不同的范围内

Simple.

一个正常函数是这样的:

var logMyName = function (name) {
console.log(name);
}; logMyName('Todd');

我们可以选择调用它,在任何我们需要/想的位置。

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

这意味着它们在运行时被立即调用,

我们也不能再调用它们了,它们只运行一次:

var logMyName = (function (name) {
console.log(name); // Todd
})('Todd');

这里的秘诀是,(我在前面的例子中给一个变量赋值):

(function () {

})();

多余的一对括号是必要的,因为这样不起作用:

function () {

}();

虽然可以通过一些技巧来欺骗JavaScript“使其工作”。
这样强制JavaScript解析器处理 ! 后面的代码:

!function () {

}();

还有其他的变体:

+function () {

}();
-function () { }();
~function () { }();

但我不会用它们。

请参阅@ mariusschulz分解JavaScript的IIFE语法,详细解释IIFE语法及其变体。

https://blog.mariusschulz.com/2016/01/13/disassembling-javascripts-iife-syntax

现在我们知道了它是如何运作的,我们可以将论证传递给我们的 IIFE:

(function (window) {

})(window);

它是如何工作的呢?
记住, (window); 是调用函数的地方,
我们通过窗口对象。
然后这个函数被传递到函数中,我也把它命名为window。
你可以认为这是毫无意义的,因为我们应该给它命名不同的东西,但是现在我们也将使用窗口。

我们还能做什么呢?把所有的东西都传过去!让我们通过文档对象:

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

那么关于 undefined 呢?

在ECMAScript 3中,未定义的是可变的。

这意味着它的值可以被重新分配,比如undefined = true;

oh my! 幸运的是,在 ECMAScript 5 中的 ('use strict';)语法将会抛出一个错误告诉你你是一个白痴。

在此之前,我们开始保护自己的 IIFE:

(function (window, document, undefined) {

})(window, document);

也就是说,如果有人来做这件事,我们会没事的:

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);

想象一下,你对函数库、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();
或者任何东西作为$被分配到模块的本地。
了解范围和全局/局部变量如何工作将进一步帮助您。

还剩下一小段,不想看了,饿了。感觉好像被塞了一把JS的知识。

原因只是我想知道这样一段代码什么意思。

(function (angular) {
'use strict'; //do something })(window.angular);

JS (function (window, document, undefined) {})(window, document)的真正含义的更多相关文章

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

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

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

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

  3. (function (window, document, undefined) {})(window, document)什么意思?

    1.IIFE(即时调用的函数表达式),它采取以下表达式: (function (window, document, undefined) { // })(window, document); Java ...

  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. TC-572-D1L2 未完!待续!

    题目描述 • 有一个神秘的常数 K ,s 位• 现在有 n 个 s 位数,告诉你每个数与 K 有多少位是相同的• 判断 K 的无解.多解.唯一解,并求出唯一解(如果存在的话)• 所有出现的数都允许前导 ...

  2. php imagecreatetruecolor()方法报未定义错误解决方法

    更多内容推荐微信公众号,欢迎关注: php练习生成验证码方法时,使用php的 imagecreatetruecolor() 方法 报错 Fatal error: Uncaught Error: Cal ...

  3. NodeJS让前端与后端更友好的分手

    学问   最近“上层建筑”在兴起国学热,所以公司几个月前决定开发一款名叫“学问”的有关于国学的app.     APP的详情页面还是由web来显现具体内容,有些类似于新闻页,图文混排什么的web是最适 ...

  4. Shiro认证的另一种方式

    今天在学习shiro的时候使用另一种shiro验证的方式. 总体的思路是: (1)先在自己的方法中进行身份的验证以及给出提示信息.(前提是将自己的验证方法设为匿名可访问) (2)当验证成功之后到Shi ...

  5. Hibernate5笔记6--Hibernate检索优化

    Hibernate检索优化: 检索即查询.为了减轻DB的访问压力,提高检索效率,Hibernate对检索进行了优化. 所谓检索优化,指的是对查询语句的执行时机进行了细致.严格的把控:并不是代码中一出现 ...

  6. go 切片练习

    下列程序输出什么? package main import "fmt" func main() {     var sa = make([]string, 5, 10)     f ...

  7. MySQL 四种链接

    1.内联接 INNER JOIN(典型的联接运算,使用像 =  或 <> 之类的比较运算符).包括相等联接和自然联接.     内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行. ...

  8. 十五、springboot集成定时任务(Scheduling Tasks)(二)之(线程配置)

    配置类: /** * 定时任务线程配置 * */ @Configuration public class SchedulerConfig implements SchedulingConfigurer ...

  9. Gradle详解

    一.脚本文件(build.gradle)  项目与脚本文件 当我们执行gradle命令的时候,Gradle会在你执行命令的目录下寻找一个名为build.gradle的文件,这个文件就是Gradle的脚 ...

  10. go语言版本变化

    The Go Project     Go is an open source project developed by a team at Google and many contributors  ...