Building a JavaScript library, why use an IIFE this way?

余生长醉 提交于 2019-11-30 03:38:40
Michał Perłakowski

This code pattern is called Universal Module Definition (UMD). It allows you to make your JavaScript library usable in different environments. It provides three ways of defining modules:

  1. Asynchronous Module Definition (AMD), implemented by RequireJS and Dojo Toolkit.

    define( [], factory );

  2. CommonJS — NodeJS modules.

    module.exports = factory();

  3. Assigning module to the global object, for example window in browsers.

    root[globalName] = factory();

The IIFE has three parameters: globalName, root and factory.

  • globalName is the name of your module. It applies only to the third way of defining a module, i.e. assigning your module object to the global variable. For example, if you set this parameter to "myAwesomeModule" and use the code in browser (without AMD), you can access your module using myAwesomeModule variable.
  • root is the name of global object. Obviously, it also applies only to the third way of defining a module. Usually this is passed as this parameter, because this is a reference to window in browser. However, this doesn't work in strict mode. If you want your code to work in strict mode, you can replace this with typeof window !== "undefined" ? window : undefined.
  • Finally, factory is an anonymous function, which should return your module as object.

See also:

This is an example of Universal Module Definition (UMD). It is a technique to make a JS module compatible with the three popular JS module specs:

  1. Asynchronous Module Definition (AMD, used by Require.js)

    define('name', [ /* dependencies */ ], factory);
    
  2. CommonJS (Node.js ecosystem)

    module.exports = object;
    
  3. Global exports (for example, on window in the browser)

    global['name'] = object;
    

UMD wraps a factory function responsible for creating the object to be exported and passes it as an argument to an immediately invoked function expression (IIFE), as in the snippet you pasted. The IIFE is responsible for detecting the module environment, and exporting the object created by the factory in an appropriate way. The pattern is as follows:

(function (name, root, factory) {
   // detect the module environment and
   // export the result of factory()
})('name', this, function () {
   // module code
   // return the object to be exported
});

Many transpilers and build tools generate this wrapper automatically.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!