最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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 my more procedural code that gets executed only in one place.


Example

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

I find this preferable to both:

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

since over time the ment may get separated from the code and its not immediately obvious what line(s) the ment applies to

and to:

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

hideStuffOnInstantiaton();

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


Are there any performance, maintainability, testability, or cross-browser considerations when using this pattern? I don't believe I've seen many people use this in the wild butI feel as though it could be very useful

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 my more procedural code that gets executed only in one place.


Example

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

I find this preferable to both:

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

since over time the ment may get separated from the code and its not immediately obvious what line(s) the ment applies to

and to:

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

hideStuffOnInstantiaton();

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


Are there any performance, maintainability, testability, or cross-browser considerations when using this pattern? I don't believe I've seen many people use this in the wild butI feel as though it could be very useful

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Mar 20, 2013 at 17:17 Zach LysobeyZach Lysobey 15.7k21 gold badges100 silver badges151 bronze badges 8
  • I would not name my immediately-invoked function. If I see a code like yours I would not expect it to be immediately invoked, because you can have (function a(){ .. });. I would perfer a ment instead. – TryingToImprove Commented Mar 20, 2013 at 17:24
  • 5 It might be useful for debugging, as the function name will show on the stack trace. Also, this is a pulsory reading on this topic: kangax.github./nfe – bfavaretto Commented Mar 20, 2013 at 17:27
  • @bfavaretto - Thanks for the link I'll start digging in. – Zach Lysobey Commented Mar 20, 2013 at 17:36
  • @TryingToImprove - Never seen that before, I'm sure the above link will have the answer, but can you explain briefly why you would ever do that without invoking the function? – Zach Lysobey Commented Mar 20, 2013 at 17:37
  • 1 @BishopZ where specifically? Also, why not link directly to coding.smashingmagazine./2012/11/05/… ? – blong Commented Mar 21, 2013 at 19:21
 |  Show 3 more ments

2 Answers 2

Reset to default 8 +100

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.

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 ments in function names, I'd call that a bad practise. If someone separates the ment from the code, it's his fault not yours - actually he could pack pletely 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 ment applies to a block of code, you can explicitly use a block statement for that and put your ment 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.

发布评论

评论列表(0)

  1. 暂无评论