开发者

Variable Undefined in Anonymous Function

function updateServerList() {
    var i;

    for (i=0; i < servers.length; i++) {
     var server = servers[i];
     var ip = server['serverIp']


     var html = constructServer(i);
     var divId = '#server' + ip.replace(new RegExp("\\.", "mg"), "-");
     var visible = $(divId).find(".server_body").is(":visible");
     var div = $(divId);
     div.html(html);

     // Set div class.
     var prevState = div.attr('class').substring(7)
     if (prevState != server['state']) {

         if (server['state'] == 'ok') {
         console.debug(server);
         div.slideUp('fast', function(server) {
             $(this).removeClass();
             $(this).addClass('server_ok');
             var id = ipToId[server['serverIp']];
             console.debug(id);
             i开发者_如何学Gof (id == 0) {
             adjacentIp = servers[1]['serverIp'];
             adjacentDivId = '#server' + adjacentIp.replace(new RegExp('\\.', 'g'), '-');
             $(adjacentDivId).before(this);
             }
        }).delay(1000);
        div.slideDown();
        }
    }
}

console.debug shows server as being defined, but inside the anonymous function, server is not defined. What am I going wrong?


because server is an argument to the function, its masking the value of the server at the higher level. You need to either pass server to the function, or remove the function argument. I would do the latter, as slideUp doesn't give you a way to pass arguments. You could do it but its needlessly complicated; it would look something like the following

div.slideUp('fast', (function(server) { 
   return function(){ 
      // your stuff here, server is now 'closed in', i.e. in a closure
   }
})(server)); // <-- this server is the current value in the loop

what you are doing here is invoking a new function right away, passing in the argument server, and returning a new function that receives that value.


var server = servers[i];

var prevState = div.attr('class').substring(7);

if (prevState != server['state']) {

   if (server['state'] == 'ok') {
      console.debug(server);
      div.slideUp('fast', function() {
            ...
         var id = ipToId[server['serverIp']];
     }
}

Inside your anonymous function, "server" is still within the function scope. No need to pass it in as an argument.


The Quick Fix

// ...

div.slideUp('fast', function() { // `server` argument removed
    // ...
});

The Explanation

There is no need to pass server to the function. The anonymous function "closes" over the server variable.


This is merely a function declaration:

function (server) {...}

You aren't passing anything to the function yet, as it isn't being invoked yet! The (server) bit in a function declaration simply lets you name the arguments to your function. Only when you invoke the function can you pass arguments:

var name = "Jill";
var showName = function (name) {
    alert(name);
};

showName("Jack"); // alert box shows "Jack"
showName(); // alert box shows "undefined"

So, when you declare that the name of the first argument to your anonymous function is server, there is a name conflict which prevents the original from being accessible; the server in your anonymous function is whatever slideUp passes as the first argument, which, according to the documentation, is nothing, so server is now undefined.

If this is confusing (and I suspect it is), I would suggest reading about javascript closures. Here's a good place to get started.


Fun fact: you can actually access arguments, in order, without having any explicit names, by using Javascript's built in arguments array object inside a function:

var sum = function () {
    var i, total = 0;
    for(i = 0; i < arguments.length; ++i) {
        total = total + arguments[i];
    }
    return total ;
};

alert(sum(1,2,3)); // Displays "6"
alert(sum(1,2,3,4)); // Displays "10"
alert(sum(1,0,2,3)); // Displays "6"
alert(sum()); // Displays "0"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜