Eduard Luca Eduard Luca - 1 year ago 53
Javascript Question

Javascript call nested function

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 Source

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download