Using Named Immediately-Invoked Function [removed]IIFE) instead of comments

后端 未结 2 1722
轻奢々
轻奢々 2021-01-02 05:24

What are the pros and cons of utilizing Named IIFEs within JS code to describe and group related code?

I\'ve been using this "pattern" to lend structure to

2条回答
  •  一生所求
    2021-01-02 06:00

    why separate the function and its execution if its only executed in one place?

    In that case, there's no reason to use a function (with its overhead) at all - just inline the code. And don't hide comments in function names, I'd call that a bad practise. If someone separates the comment from the code, it's his fault not yours - actually he could pack completely unrelated stuff in your IENFE as well.

    While this pattern could be useful if you'd reuse the function (recursively) or need it to build a closure around something, and the named function makes stacktraces easier to debug, there are various bugs in IE. So avoid it if you don't really need it - and you do not.

    If you want to express that your comment applies to a block of code, you can explicitly use a block statement for that and put your comment at its head:

    { // hide Stuff on Instantiaton
        $('oneThing').hide().removeClass();
        $('#somethign_else').slideUp();
        $('.foo').fadeOut();
    }
    

    …although that'll likely confuse your readers as much as a superfluous IEFE.

提交回复
热议问题