0

If I run the following...

function outer(){
    function inner(){
    }

    inner();
}

inner will run the first time as expected.


If I then try to run the following...

function outer(){
    function inner(){
        inner();
    }

    inner();
}

inner will again run the first time as expected, but then fails at any subsequent attempts, returning the error: ReferenceError: inner is not defined


If I expose the function to the global namespace it will work again...

function outer(){
    function inner(){
        inner();
    }

    window.inner = inner;
    inner();
}

Is there a way to reference the nested function from within itself WITHOUT adding it to the global namespace?

1 Answer 1

4

You can assign the function to a local variable within the execution scope

function outer() {
    var inner;

    inner = function () {
        inner();
    }
    inner();
}
Sign up to request clarification or add additional context in comments.

2 Comments

hm, I am still receiving 'inner is not defined' error message. I am calling inner from a setTimeout inside inner if that makes a difference.
Are you using quotes for the setTimeout? you don't need to, you can just pass any function instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.