What is the difference between these two functions/approaches?

前端 未结 4 1514
南旧
南旧 2020-12-06 07:10

I use only jQuery for writing JavaScript code. One thing that confuses me is these two approaches of writing functions,

First approach



        
4条回答
  •  春和景丽
    2020-12-06 07:49

    The function declaration syntax cannot be used within a block statement.

    Legal:

    function a() {
        function b() {
    
        }
    }
    

    Illegal:

    function a() {
        if (c) {
            function b() {
    
            }
        }
    }
    

    You can do this though:

    function a() {
        var b;
        if (c) {
            b = function() {
    
            };
        }
    }
    

提交回复
热议问题