JavaScript: self-executing function with parameter

CodeMirror.net uses this construct (I'm simplifying a bit) to enter the code for its JavaScript editor:

(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 read several posts about them. I understand that in practice this code creates a CodeMirror object. I just don’t understand the mechanics.

  • What is the role of the parameter (mod)? In a broader sense, what does it mean when you give a parameter to a self-executing function?
  • What is the role of declaring an internal function ()? Does this seem to be related to the mod in any way?

Thank you for your help.

+4
source share
2 answers

In your code:

(function(mod) {
    this.CodeMirror = mod();
})(function() {
  "use strict";
   (15,000-odd lines of advanced JS)
}

mod . ​​ :

function something(mod) {
    this.CodeMirror = mod();
}

, mod - , , , .

, , mod, :

function() {
  "use strict";
   (15,000-odd lines of advanced JS)
}

, , () , CodeMirror.

— one-mdash; - this, , this window . , :

(function(mod) {
    this.CodeMirror = mod();
}).call(this, function() {
  "use strict";
   (15,000-odd lines of advanced JS)
})

, this , "" . , this undefined "" , .

+5
(function(mod) {
  this.CodeMirror = mod();
})(function() {
    "use strict";
    //(15,000-odd lines of advanced JS)
})

, :

  • (function(mod) { this.CodeMirror = mod(); }) mod.
  • this.CodeMirror = mod(); mod , , , . RETURN Window.CodeMirror. self invoking this, Window Object.
  • , .

: , 15000 , Window.CodeMirror

+2

Source: https://habr.com/ru/post/1607103/


All Articles