[英]JavaScript: Self-executing function with parameter
CodeMirror.net uses this construct (I'm simplifying slightly) to introduce the code for its JavaScript editor: CodeMirror.net使用这个构造(我稍微简化)来为它的JavaScript编辑器引入代码:
(function(mod) {
this.CodeMirror = mod();
})(function() {
"use strict";
(15,000-odd lines of advanced JS)
}
Now, I understand that this is a self-executing function, and I've read a number of posts about them. 现在,我知道这是一个自动执行的功能,我已经阅读了很多关于它们的帖子。 I understand that, in practice, this code is creating a CodeMirror object.
据我所知,实际上,这段代码正在创建一个CodeMirror对象。 I just don't understand the mechanics.
我只是不懂机制。
Thanks for your help. 谢谢你的帮助。
In your code: 在你的代码中:
(function(mod) {
this.CodeMirror = mod();
})(function() {
"use strict";
(15,000-odd lines of advanced JS)
}
mod
is the formal parameter to the immediately-invoked function. mod
是立即调用函数的形式参数。 It's as if the function were declared in a more straightforward way: 这就好像函数以更直接的方式声明:
function something(mod) {
this.CodeMirror = mod();
}
That function clearly expects that the mod
parameter will be a reference to some other function, because the only thing it does is make a function call with it. 该函数显然期望
mod
参数将是对其他函数的引用,因为它唯一做的就是用函数调用它。
The immediately-invoked function is, in fact, called with a function as the value of the mod
parameter: specifically, this one: 事实上,使用函数调用立即调用的函数作为
mod
参数的值:具体来说,这个:
function() {
"use strict";
(15,000-odd lines of advanced JS)
}
That function does whatever it does, and (presumably) returns an object reference to be used as the global CodeMirror
entry point. 该函数执行任何操作,并且(可能)返回一个对象引用以用作全局
CodeMirror
入口点。
Because the first function — the immediately-invoked one — is invoked without any explicit this
value, it expects that this
will be set to the value of the global context, or window
in a browser. 由于第一功能-紧邻的调用一个-在没有任何明确援引
this
值,它预期this
将被设置为全球范围内的值,或window
在浏览器中。 Personally I think it would be safer to do that explicitly: 就个人而言,我认为明确地这样做会更安全:
(function(mod) {
this.CodeMirror = mod();
}).call(this, function() {
"use strict";
(15,000-odd lines of advanced JS)
})
In the global lexical context, it is guaranteed that this
will be a reference to the global context, "strict" mode or not. 在全球范围内的词汇,可以保证
this
将是全球范围内,“严”模式或不是一个引用。 But if that outer immediately-invoked function is simply called, then this
will be undefined
in "strict" mode and the initialization would fail. 但是如果简单地调用外部立即调用的函数,那么在“严格”模式下
this
将是undefined
,并且初始化将失败。
(function(mod) {
this.CodeMirror = mod();
})(function() {
"use strict";
//(15,000-odd lines of advanced JS)
})
Nothing weird or magical is happening here, Here is the flow of this: 这里没有任何奇怪或神奇的事情,以下是这个流程:
(function(mod) { this.CodeMirror = mod(); })
This declares an anonymous function and it accepts the parameter mod
. (function(mod) { this.CodeMirror = mod(); })
这声明了一个匿名函数,它接受参数mod
。 this.CodeMirror = mod();
this.CodeMirror = mod();
takes the mod
and invoke it like a method, implying that programmer's expects mod to be a function. mod
并像方法一样调用它,暗示程序员期望mod是一个函数。 The RETURN value of that method is assigned to Window.CodeMirror object. this
set to Window
Object. this
设置Window
对象。 Summary: The Result of function that has 15000 lines of code is assigned to Window.CodeMirror
摘要:将具有15000行代码的函数结果分配给
Window.CodeMirror
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.