Difference between anonymous functions

I read javascript open source frameworks and found more ways to create anonymous functions, but what makes the best way different?

(function() {
    this.Lib = {};
}).call(this);

(function() {
    var Lib = {}; window.Lib = Lib;
})();

(function(global) {
    var Lib = {}; global.Lib = Lib;
})(global);
+3
source share
3 answers
(function() {
    this.Lib = {};
}).call(this);

Defines the property of the Libobject it called on, and immediately calls thiswhich it usually is window. It can instead refer to the object that owns the method in which it is called.

(function() {
    var Lib = {}; window.Lib = Lib;
})();

Defines a property Lib windowregardless of where it called (although it also called right away).

(function(global) {
    var Lib = {}; global.Lib = Lib;
})(global);

Lib , . , , global . window .


" ", . ( ) . .

, , , , // ( , Lib).

undefined, ( new), , , , .

+3

( ):

var Lib = {};

(IIFE) , , , .

, , , :

(function(global) {
  ...
})(this);
+1

All Articles