2

In javascript, I can use foo.caller to get a reference to the function above foo in the stack trace. However, that doesn't work when a function appears multiple times in the same stack trace, foo.caller just returns foo.

Is there a stable, cross-platform method of getting a full stack trace in Javascript? I do not want to get a printable stack trace; rather, I'm doing stack inspection to see if a certain method is anywhere above me in the stack. Here's my current code:

function inFunction(foo) {
    var caller = inFunction.caller;
    var maxDepth = 20;
    while(caller && --maxDepth > 0) {
        if(caller == foo)
            return true;
        caller = caller.caller;
    }
    return false;
}

Any ideas how to deal with a function existing multiple times in the stack trace?

2

1 Answer 1

5

Sorry, but there's not. Once you hit a recursive function in the trace it is impossible to get to the calling function.

I spent several days trying to come up with work-arounds to this while writing an IDE in JavaScript for Sun Microsystems. There are none.

Sign up to request clarification or add additional context in comments.

Comments

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.