Passing arguments in anonymous functions in JavaScript

后端 未结 5 1115
遥遥无期
遥遥无期 2021-02-02 11:29

Sometimes I see JavaScript that is written with an argument provided that already has a set value or is an object with methods. Take this jQuery example for instance:



        
5条回答
  •  野性不改
    2021-02-02 11:43

    Here's a example of passing parameters into anonymous function

        var a = 'hello';
    
        // Crockford
        (function(a){alert(a)}(a));
    
        // Others
        (function(a){alert(a)})(a);
    

    It uses closure, since it's an anonymous function (it actually all depends how you wrote it)

提交回复
热议问题