gpt4 book ai didi

javascript:声明并立即调用内联函数的原因

转载 作者:行者123 更新时间:2023-11-30 11:57:22 25 4
gpt4 key购买 nike

所以我有一个采访,我问的是立即声明和调用函数的目的,我无法回答,即:

(function(){

// code
})();

这样做的原因是什么?

最佳答案

Object-Oriented JavaScript - Second Edition: One good application of immediate (self-invoking) anonymous functions is when you want to have some work done without creating extra global variables. A drawback, of course, is that you cannot execute the same function twice. This makes immediate functions best suited for one-off or initialization tasks.

The syntax may look a little scary at first, but all you do is simply place a function expression inside parentheses followed by another set of parentheses. The second set says "execute now" and is also the place to put any arguments that your anonymous function might accept:

(function() {

})();

(function() {

}());

相同:

关于javascript:声明并立即调用内联函数的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37546809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com