在 JS 代码中使用命名 IIFE 来描述和分组相关代码的优缺点是什么?
我一直在使用这种“模式”为我的更多过程代码提供结构,这些代码只在一个地方执行。
例子
(function hideStuffOnInstantiaton(){
$('oneThing').hide().removeClass();
$('#somethign_else').slideUp();
$('.foo').fadeOut();
}());
我发现这比两者更可取:
// hide Stuff on Instantiaton
$('oneThing').hide().removeClass();
$('#somethign_else').slideUp();
$('.foo').fadeOut();
随着时间的推移,注释可能会与代码分离,并且注释适用于哪一行并不是很明显
和:
function hideStuffOnInstantiaton(){
$('oneThing').hide().removeClass();
$('#somethign_else').slideUp();
$('.foo').fadeOut();
};
hideStuffOnInstantiaton();
因为如果它只在一个地方执行,为什么要将函数和它的执行分开?
使用此模式时是否有性能、可维护性、可测试性或跨浏览器方面的考虑?我不相信我见过很多人在野外使用这个,但我觉得它可能非常有用
最佳答案
我一直认为标签很酷:
hideStuffOnInstantiaton: {
$('oneThing').hide().removeClass();
$('#somethign_else').slideUp();
$('.foo').fadeOut();
}
实际上,这样做通常很愚蠢。相反,分组功能通常属于其自己的功能。
关于javascript - 使用命名立即调用函数表达式 (IIFE) 而不是注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15530374/