vendredi 29 juillet 2016

Add a method to a function that calls the function

Creating a module I ended designing a pattern where I attach methods to a function, and I'm not sure if it is correct. It is a closure that returns a function that has some methods attached which in turn calls the function itself. I don't know if this is a bad practice or if it is considered to be ok. My objective is to provide ways to call the function with certain presents or in different ways, but I want to retain the ability to just call the function in its simpler form. Would this lead to memory leaks or anything like that?

I'm not making use of this at any point, so no danger of losing context.

Below you can find a code snippet with a simplified version.

function factory( general ){

    var pusher = setTimeout(function(){ console.log('$',general) },1000);
    var counter = 0;

    function reporter ( specific ){
        counter++;
        console.log(counter, general , specific)
    }

    reporter.middleware = function ( something ){
        clearTimeout(pusher);
        return factory ( general + something )
    }

    return reporter
}

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire