为什么将 IIFE 分配给变量? [英] Why assign an IIFE to a variable?

查看:33
本文介绍了为什么将 IIFE 分配给变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在 JavaScriptAngularJS 中使用 IIFE,并且一直使用以下结构:

I've been using IIFE in JavaScript and in AngularJS and have been using the following structure:

方法一:

//IIFE Immediately Invoked Function Expression
(function () {


}());

但是,我经常看到以下情况,其中将变量分配给 IIFE

However, I've seen the following often where a variable is assigned to the IIFE

方法二:

//IIFE Immediately Invoked Function Expression assigned to doStuff variable
var doStuff = (function () {


}());

注意:这个问题不是关于这种模式是什么或IIFE是什么.这特别涉及为什么要在 IIFE 上使用返回变量,以及它与 Angular 实践的关系.

NOTE: This question is NOT about what this pattern is or what an IIFE is. This is pertaining specifically to why one would use a return variable on an IIFE and its relation to Angular practices as well.

在 Angular 方法 1 中工作正常,但在我看到的许多原始 JS 示例中,使用了方法 2.我的假设是任何封装在 doStuff 中的东西都可以通过它访问和调用.但是,我不能 100% 确定这两种方法之间的确切推理或区别,需要帮助理解何时使用不同的方法?

In Angular Method 1 works fine, but in many of the raw JS examples I see, Method 2 is used. My assumption is that anything encasulated in doStuff will be avliable via it and callable. However, I'm not 100% sure on the exact reasoning or distinction between the 2 methods and need some help understanding when to use the different methods?

推荐答案

方法 #2 的原因是您会在 IIFE 中找到返回某些东西的代码(通常,但不一定,一个对象或一个函数).IIFE 返回的是最终分配的内容.例如:

The reason for Method #2 is that you'll find code within the IIFE that returns something (typically, but not necessarily, an object or a function). What the IIFE returns is what ends up being assigned. E.g.:

//IIFE Immediately Invoked Function Expression assigned to doStuff variable
var doStuff = (function () {
    var privateInformationForDoStuff = 0;

    function doStuff() {
        console.log(++privateInformationForDoStuff);
    }

    return doStuff;
}());

在那里,变量最终成为对函数的引用,每次调用该函数时,都会给我们一个比前一次大的数字.IIFE 是为了确保没有任何东西可以修改 privateInformationForDoStuff 变量,它对 doStuff 函数来说是完全私有的.

There, the variable ends up being a reference to a function that, each time it's called, gives us a number higher than the previous time. The IIFE is there to ensure that nothing can modify the privateInformationForDoStuff variable, it's entirely private to the doStuff function.

使用它(有时称为显示模块模式")来创建具有各种功能的对象是很常见的,这些功能也可能具有仅在模块"内共享的私人信息:

It was common to use this (sometimes called the "revealing module pattern") to create objects with various functions on them which might also have private information that's only shared within the "module":

var MyApp = (function() {
    var privateModuleInformation;
    var morePrivateModuleInformation;
    // ...

    function doThis() {
        // ...
    }

    function doThat() {
        // ...
    }

    function doTheOther() {
        // ...
    }

    return {
        doThis: doThis,
        doThat: doThat,
        doTheOther: doTheOther
    };
})();

在现代环境中,您将使用实际模块 代替,但这在过去不是一个选项.

In modern environments you'd use an actual module instead, but that didn't used to be an option.

这篇关于为什么将 IIFE 分配给变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆