Ambiguous function declaration in Javascript

前端 未结 4 1284
面向向阳花
面向向阳花 2020-12-19 00:10

I am new to Javascript and got confused by how the function declaration works. I made some test on that and got some interesting results:

say();

function sa         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 01:04

    Its always good idea calling the function later, even though javascript works like that.

    Most of the languages won't work that way, instead do this.

    function say(){
        alert("say");
    }
    
    say();
    

    or

    say = function(){
        alert("say");
    }
    
    say();
    

    or

    (function(){
        alert("say");
    })();
    

提交回复
热议问题