1

I am having a strange problem with global variables disappearing on me. Here is some stripped down semi-pseudo-code:

var globy = 99;

jQuery.get("file", function(){
  check();
})

function check(){
 main();
}

function main(){
 forloop
  whileloop
   forloop
    while(test()){}
}

function test(){
  //globy causes a reference error here
}

That should explain the code structure. All the way up to the test() function "globy" is fine but suddenly inside test() all global variables disappear. What the heck?

0

2 Answers 2

2

If you are wrapping everything in a $(document).ready() but have your test() function outside of the document ready you will have a scoping issue.

$(document).ready(function () {
    var globy = 99;

    jQuery.get("file", function () {
        check();
    })

    function check() {
        main();
    }

    function main() {
        forloop
        whileloop
        forloop
        while (test()) {}
    }
});

function test() {
    //globy causes a reference error here
}

Besides that it will be hard to say without the real js.

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

1 Comment

Aha. You are right! I had test() actually in a separate file that I was including in at the top. I guess for now I can leave the document ready part out. Would getScript() allow me to pull the external script inside the $(document).ready()?
1

Your test() function is probably defined outside the scope where globy resides. Without more detail, it's hard to say.

1 Comment

A way to circumvent this is to use window.globy. If you must use globals, that is.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.