what is self-executing anonymous function or what is this code doing?

前端 未结 4 978
生来不讨喜
生来不讨喜 2020-12-24 09:43
var module = {};


(function(exports){

  exports.notGlobalFunction = function() {
    console.log(\'I am not global\');
  };  

}(module));

function notGlobalFunct         


        
4条回答
  •  囚心锁ツ
    2020-12-24 10:45

    "self-executing" might be misleading. It is an anonymous function expression, that is not assigned or or given as an argument to something, but that called. Read here on Immediately-Invoked Function Expression (IIFE).

    what is var module = {} doing?

    It initializes an empty object that is acting as a namespace.

    why is it called again inside the fist function?

    It is not "called", and not "inside" the first function. The object is given as an argument ("exports") to the IEFE, and inside there is a property assigned to it.

提交回复
热议问题