【问题标题】:Using functions outside an IIFE function使用 IIFE 函数之外的函数
【发布时间】:2026-02-09 04:30:01
【问题描述】:

我有以下方法,它在 someFile.js 里面:

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(['b'], factory);
    } else if (typeof exports === 'object') {
        // Node. Does not work with strict CommonJS, but
        // only CommonJS-like environments that support module.exports,
        // like Node.
        module.exports = factory(require('b'));
    } else {
        // Browser globals (root is window)
        root.returnExports = factory(root.b);
    }
}(this, function (b) {
    //use b in some fashion.

    // Just return a value to define the module export.
    // This example returns an object, but the module
    // can return a function as the exported value.

        b.isValid = function(parameter){
            if (!isString(parameter)){
                return false;
            }
            parameter = this.newFormat(parameter);
            return parameter;
        };

}));

作为一个 IIFE 函数,它会自动调用自己,但是,我想在一个单独的 JavaScript 文件中做的是使用该方法,例如:

b.isValid('value to test');

这可能吗?或者从这个 IIFE 函数之外访问或调用这些函数的最佳解决方案是什么?

提前致谢。

【问题讨论】:

  • 您可以将其分配给window['b'] 或将其返回给IIFE 之外的变量?
  • 您是否只是偶然使用了 UMD?
  • 只要使用完全相同的模式,它就会神奇地工作。您在导入后将一个方法添加到全局 b 模块中,您可以以完全相同的方式将其导入其他文件中。
  • 我不认为以这种方式改变(或扩展)依赖项是一个好主意(除非b 是某个库并且module 是某个插件或类似的东西)

标签: javascript function iife


【解决方案1】:

您可以从工厂函数返回一些值,然后将其分配给exports 对象或根(或其他)...

例如module.js

(function (root, factory) {
    if (typeof define === 'function' && define.amd) {
            // AMD. Register as an anonymous module.
            define(['dependency'], factory);
        } else if (typeof exports === 'object') {
            // Node. Does not work with strict CommonJS, but
            // only CommonJS-like environments that support module.exports,
            // like Node.
            module.exports = factory(require('dependency'));
        } else {
            // Browser globals (root is window)
            root['identifier']= factory(root['dependency']);
        }
    })(this, function (dep) {

        // Just return a value to define the module export.
        // This example returns an object, but the module
        // can return a function as the exported value.

        return {
           fn: function(){
                console.log([].slice.call(arguments))
            }
        }

    });

index.html

<script src="module.js"></script>
<!-- Module will be executed in the global context and it's value will be assigned to the window object -->
<script>
    // can access the module's export here
    window['identifier'].fn('something', 'goes', 'here')
    // or, preferred way would be: identifier.fn('something', 'goes', 'here')
</script>

或在 some_other_module.js

// CommonJS
var depX = require("./module.js")  // can omit .js extension
depX.fn('something', 'goes', 'here')

// es6
import depX from "./module.js"  // can omit .js extension
depX.fn('something', 'goes', 'here')

【讨论】: