job立刻执行
JavaScript is full of nifty little tricks to accomplish tasks with less code. One of those tricks is immediately executing functions. We oftentimes see this pattern for executing anonymous functions to limit variable scope:
JavaScript充满了漂亮的小技巧,可以用更少的代码来完成任务。 这些技巧之一就是立即执行功能。 我们经常看到这种执行匿名函数以限制变量范围的模式:
(function() {
console.log('executed!');
// Do processing here
})();
What many developers don't know is that this code can be shorted by using a ! before the anonymous function:
许多开发人员不知道的是,可以使用!来缩短此代码! 在匿名函数之前:
!function() {
console.log('executed!');
// Do processing here
}()
The function above executes immediately, just as the first snippet did. One caveat: the immediately executing function always returns false. If you desire the result of the anonymous function, you wont want to use this second pattern.
上面的功能会立即执行,就像第一个代码片段一样。 一个警告:立即执行的函数始终返回false 。 如果需要匿名函数的结果,则不想使用第二种模式。
Ben Alman has created an excellent, detailed writeup on the subject and if you want to learn more, be sure to visit his post!
本·阿尔曼(Ben Alman)为此主题撰写了一篇出色,详尽的文章,如果您想了解更多信息,请务必访问他的帖子!
翻译自: https://davidwalsh.name/immediately-executing-functions
job立刻执行