I had a problem with some JavaScript functions that had me scratching my head for about an hour until some well-placed alert()'s revealed something which surprised me. One function was changing another function's local variables, it seems. I wrote a simple test script:
function first() {
msg = "1111";
second();
alert(msg);
}
function second() {
msg = "2222";
}
When I call first() I'd expect to get an alert box saying "1111" but I get "2222" instead. How is it that second() is affecting a local variable belonging to first()? Am I missing something or is this a bug?
I'm using Firefox 12.0.