I see IIFE's all the time.

I see a series of statements that are left open, i.e, they are not encapsulated by brackets.

Why have I not seen a function followed by its invocation

    // lib code...we are inside an IIFE 

    function fooName () {
    }
    fooName();

    // lib code...

to immediately invoke a series of statements when you don't need closure, i.e. you don't need persistent or static local variables?

Is there something wrong with this idiom/pattern? Does it have name? Is it used?

Non - "small" code

function manageGlobal() {
    if (win.$A && win.$A.cg) {
        $A.extend($A, window.$A);
    } else if (window.$A) {
        $A_previous = window.$A;
    } else {
        $A = window.$A = {};
    }
}
manageGlobal();
有帮助吗?

解决方案

This is just a function declaration followed by its call.

It has no name as it has no specificity : it's just basic obvious use of the language.

By the way, it has no advantage over an IIFE :

  • It's more verbose
  • It adds a name to the global (or local) namespace.

If you don't need a closure, and don't need to avoid namespace polluting as you're in a small IIFE, then you may just directly include the code you have in your function instead of declaring it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top