admin管理员组

文章数量:1334122

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 piling 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???

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 piling 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???

Share Improve this question edited Oct 18, 2013 at 7:02 kmdhrm asked Oct 18, 2013 at 6:54 kmdhrmkmdhrm 5352 gold badges6 silver badges17 bronze badges 11
  • 4 "While piling I get the error" Compiling? With what? – T.J. Crowder Commented Oct 18, 2013 at 6:55
  • They're not. They're defined in the for scope. – David Hedlund Commented Oct 18, 2013 at 6:55
  • 2 @DavidHedlund: This is JavaScript. var always creates variables at function scope (or global scope, if used outside of any function). – T.J. Crowder Commented Oct 18, 2013 at 6:55
  • Those variables should be in scope, given that JS has function-level scope rather than block scope, though they'll all have the value undefined if data.length happens to be 0. I would suggest that regardless of whether it works in JS it is bad practice to declare variables inside a loop, especially if you then use them outside the loop. – nnnnnn Commented Oct 18, 2013 at 6:56
  • 2 Even absent the error from the mystery piler, this code makes no sense. 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. – T.J. Crowder Commented Oct 18, 2013 at 6:56
 |  Show 6 more ments

1 Answer 1

Reset to default 7

Update: The new version of your question pletely changes it, and makes the error from the "piler" plete 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 ment that the "piler" 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 piler, 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 plain 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.)

本文标签: javascriptHow the variables are out of scopeStack Overflow