简体   繁体   English

JavaScript:带参数的自执行功能

[英]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. 我只是不懂机制。

  1. What is the role of the parameter (mod)? 参数(mod)的作用是什么? More broadly, what does it mean when you give a parameter to a self-executing function? 更广泛地说,当您为自执行函数提供参数时,它意味着什么?
  2. What is the role of the inner function() declaration? 内部函数()声明的作用是什么? It appears that this is related to mod in some way? 看来这在某种程度上与mod有关吗?

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: 这里没有任何奇怪或神奇的事情,以下是这个流程:

  1. (function(mod) { this.CodeMirror = mod(); }) This declares an anonymous function and it accepts the parameter mod . (function(mod) { this.CodeMirror = mod(); })这声明了一个匿名函数,它接受参数mod
  2. Then the line 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. 该方法的RETURN值分配给Window.CodeMirror对象。 Self invoking functions have their this set to Window Object. 自调用函数有自己的this设置Window对象。
  3. Parenthesis right after anonymous function declaration invoke it, and in those parenthesis a function is passed a parameter. 在匿名函数声明之后立即调用它,在这些括号中为函数传递一个参数。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM