Javascript call nested function

Eduard Luca picture Eduard Luca · Jan 11, 2012 · Viewed 65.5k times · Source

I have the following piece of code:

function initValidation()
{
    // irrelevant code here
    function validate(_block){
        // code here
    }
}

Is there any way I can call the validate() function outside the initValidation() function? I've tried calling validate() but I think it's only visible inside the parent function.

Answer

Esailija picture Esailija · Jan 11, 2012

    function initValidation()
    {
        // irrelevant code here
        function validate(_block){
            console.log( "test", _block );
        }
    
        initValidation.validate = validate;
    }

    initValidation();
    initValidation.validate( "hello" );
    //test hello