Using Named Immediately-Invoked Function Expression (IIFE) instead of comments

你说的曾经没有我的故事 提交于 2019-11-30 12:06:50

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.

I always thought labels were cool:

hideStuffOnInstantiaton: {
  $('oneThing').hide().removeClass();
  $('#somethign_else').slideUp();
  $('.foo').fadeOut();
}

In reality, it's usually silly to do this. Instead, grouped functionality generally belongs in its own function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!