Can I name a JavaScript function and execute it immediately?

前端 未结 8 1734
谎友^
谎友^ 2020-11-28 04:04

I have quite a few of these:

function addEventsAndStuff() {
  // bla bla
}
addEventsAndStuff();

function sendStuffToServer() {
  // send stuff
  // get HTML         


        
相关标签:
8条回答
  • 2020-11-28 04:35

    There is a good shorthand to this (not needing to declare any variables bar the assignment of the function):

    var func = (function f(a) { console.log(a); return f; })('Blammo')
    
    0 讨论(0)
  • 2020-11-28 04:38

    Try to do like that:

    var addEventsAndStuff = (function(){
        var func = function(){
            alert('ole!');
        };
        func();
        return func;
    })();
    
    0 讨论(0)
提交回复
热议问题