mercredi 25 mai 2016

Javascript Augmented Modules: Why assign IIFE to module when passing module in as argument?

I'm new to the augmented module pattern and I've seen this example a lot:

var MODULE = (function (my) {

    my.method = function () {
        // added method...
    };

    return my;
}(MODULE));

I was wondering why I need to assign this IIFE to MODULE in the first place, when I'm passing in MODULE as an argument to the IIFE.

For example, this gives the same result:

(function (my) {

    my.method = function () {
        // added method...
    };

    return my;
}(MODULE));

(provided MODULE has already been declared).

So my question is: is either fine? If so, why do all the examples I see use the first one? It seems to me like MODULE is assigned 'method' twice. Is it just the case that MODULE is declared like this in the first example, in case it doesn't already exist?

Thanks!

Aucun commentaire:

Enregistrer un commentaire