Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the variables are out of scope?

I have the following jquery code:

$.ajax({
    url: 'somefile.php',
    type: "POST",
    data: "",
    dataType: "json",
    async: false,
    success: function (data) {
                var var1  = something;
                var var2  = something;
                var var3  = something;
                var var4  = something;
                for (var i = 0; i < data.length; i++) {
                    $('.somediv').html('');
                    $('.somediv').append('Somehtml');
                }
                some_function_declared_later(var1, var2, var3, var4);
             }

While compiling I get the error : 'var1','var2','var3' & 'var4' are used out of scope. However, I see no problem as they have been declared in the same function where they are being used.

Please help!

Update: Could this have something to do with the declaration of some_function_declared_later outside the current function???

like image 478
kmdhrm Avatar asked Mar 22 '23 00:03

kmdhrm


1 Answers

Update: The new version of your question completely changes it, and makes the error from the "compiler" complete nonsense. I find it very, very hard to believe that any tool would be giving you the error you've quoted from the (updated) code you've quoted, and all due respect, I think you must be feeding it different code than you think you are.

The original code for the success handler of your question looked like this:

function (data) {
    for (var i = 0; i < data.length; i++) {
        var var1  = data[i][0];
        var var2  = data[i][1];
        var var3  = data[i][2];
        var var4  = data[i][3];
        $('.somediv').html('');
        $('.somediv').append('Somehtml');
    }
    some_function_declared_later(var1, var2, var3, var4);
 }

...and the answer below relates to that code. With the latest version of the code in your question.

Original answer:

You've said in a comment that the "compiler" in question is "Some Online tool given by my webhost".

You're quite right, those variables are in scope. Either the tool doesn't understand JavaScript, or alternately, it does, but it's doing the "lint" thing of being more restrictive than the language. jslint, for instance, will give you an error like that ("Don't declare variables in a loop"). (Beware: jslint gives you lots of "errors" that are actually just its author's opinion about how things should be done.) In JavaScript, a variable declared with var is declared throughout the function, because JavaScript doesn't (currently) have block scope, only function scope and global scope. Your success handler code is exactly the same as this:

function (data) {
    var var1, var2, var3, var4;
    for (var i = 0; i < data.length; i++) {
        var1  = data[i][0];
        var2  = data[i][1];
        var3  = data[i][2];
        var4  = data[i][3];
        $('.somediv').html('');
        $('.somediv').append('Somehtml');
    }
    some_function_declared_later(var1, var2, var3, var4);
 }

More on my blog: Poor misunderstood var

Now, even absent the error from the mystery compiler, this code seems rather odd. You're assigning and reassigning the variables in the loop, then using them outside the loop. So they'll either be undefined (if data.length is 0) or the values from the last pass of the loop.


Re your edit:

Could this have something to do with the declaration of some_function_declared_later outside the current function???

No. If the problem were that some_function_declared_later wasn't defined as of that line of code, then the error would complain about some_function_declared_later, not the vars.

Function declarations, like var statements, are hoisted to the top of the scope where they appear. So if you have this further down:

function some_function_declared_later(a, b, c, d) {
    // ....
}

...you're fine (other than the odd loop).

If you have this further down:

var some_function_declared_later = function(a, b, c, d) {
    // ....
};

...then some_function_declared_later will be declared as of the code above it (because var is hoisted), but it may have the value undefined if the success handler runs before the line of code assigning the function to the some_function_declared_later var. (That seems unlikely, but I wouldn't write it that way, just to be sure.)

like image 60
T.J. Crowder Avatar answered Apr 01 '23 00:04

T.J. Crowder