为什么我的变量仍未定义?

时间:2013-11-06 19:17:05

标签: javascript node.js

lastbalance永远不会被定义,因此函数getBalance永远不会做它应该做的事情。我认为这可能是异步性问题,但只能是第一个间隔之前的情况,对吗?有什么想法吗?

var lastbalance;
getBalance = function (lastbalance, callback){
  btcclient.getBalance('*', 0, function(err, balance) {
    if (err) return console.log(err);
    if (lastbalance != balance || typeof lastbalance == 'undefined'){
      console.log('Last Balance:' + lastbalance);
      var lastbalance = balance;
      updateCauses();
    }
    console.log('Balance:', balance);


    if (typeof callback=='function') callback();

  });
};

setInterval(getBalance, 2000, lastbalance);

3 个答案:

答案 0 :(得分:4)

两个问题。

1。:您将lastbalance定义为函数参数...它在您的函数上下文中创建了另一个lastbalance变量...这取代了声明的变量在外部范围内。

var lastbalance; // your outer variable
getBalance = function (lastbalance, callback) { // weeeee, another lastbalance
    btcclient.getBalance('*', 0, function (err, balance) {
        if (err) return console.log(err);
        if (lastbalance != balance || typeof lastbalance == 'undefined') {
            console.log('Last Balance:' + lastbalance);
            var lastbalance = balance;
            updateCauses();
        }
        console.log('Balance:', balance);
        if (typeof callback == 'function') callback();
    });
};

setInterval(getBalance, 2000, lastbalance); //passing lastbalance by value

2。:您使用var在您的函数中声明了另一个lastbalance。不要那样做;它引起了上述同样的问题。

var lastbalance; // your outer variable
getBalance = function (lastbalance, callback) {
    btcclient.getBalance('*', 0, function (err, balance) {
        if (err) return console.log(err);
        if (lastbalance != balance || typeof lastbalance == 'undefined') {
            console.log('Last Balance:' + lastbalance);
            var lastbalance = balance; // here you create a local lastbalance.
                                       // remove the var keyword to refer to
                                       // the original lastbalance
            updateCauses();
        }
        console.log('Balance:', balance);
        if (typeof callback == 'function') callback();

    });
};

setInterval(getBalance, 2000, lastbalance);

最后,您的代码应如下所示:

var lastbalance;
getBalance = function (/*lastbalance, */callback) { // remove parameter
    btcclient.getBalance('*', 0, function (err, balance) {
        if (err) return console.log(err);
        if (lastbalance != balance || typeof lastbalance == 'undefined') {
            console.log('Last Balance:' + lastbalance);
            /*var*/ lastbalance = balance; // remove var
            updateCauses();
        }
        console.log('Balance:', balance);
        if (typeof callback == 'function') callback();
    });
};

setInterval(getBalance, 2000/*, lastbalance*/); // remove argument

答案 1 :(得分:1)

你有很多lastBalance个变量!

  • 外部范围var lastBalance =
  • 中的一个
  • getBalance函数中的一个(参数名称)
  • 回调函数var lastBalance =
  • 中的一个

这意味着当您在内部函数中执行lastBalance =时,只会设置第三个变量。没有代码可以在外部范围内设置变量。每次运行函数时,内部范围中的一个都设置为undefined

我很确定你只想要一个变量。你需要做这样的事情:

var lastbalance;
getBalance = function (callback){
  btcclient.getBalance('*', 0, function(err, balance) {
    if (err) return console.log(err);
    if (lastbalance != balance || typeof lastbalance == 'undefined'){
      console.log('Last Balance:' + lastbalance);
      lastbalance = balance;
      updateCauses();
    }
    console.log('Balance:', balance);

    if (typeof callback=='function') callback();
  });
};

setInterval(getBalance, 2000);

这可能不完全正确,因为我不熟悉您的API,但它可能没有太大的错误。

答案 2 :(得分:1)

所以这里有3个名为lastbalance的不同变量,javascript将使用范围最窄的变量。

var lastbalance;  
//creating a lastbalance var in the global scope (a member of the window object)

getBalance = function (lastbalance, callback){ 
    // <- create a function with a parameter of lastbalance. 
    // this one is a member of this getBalance function 
    // (functions in javascript are first class citezens i.e. 
    // objects too) and is different the the memeber of the 
    // window object called lastbalance.

    // that is  -- window['lastBalance'] != window.getBalance['lastBalance']; 

    var lastbalance = something   
    // create ANOTHER parameter lastbalance this time a member of the      
    // current code block, and not the same as the window member or
    // the function memeber. 

      // that is window['lastBalance'] 
      // != window.getBalance['lastBalance']
      // != window.getBalance.body['lastBalance']
};

你想要的东西看起来更像是这样。

//Create a global value. 
var lastBalance; 
getBalance = function (callback){
//this function doesnt need to be passed a variable it can read from the global scope. 
    btcclient.getBalance('*', 0, function(err, balance) {
        if (err) return console.log(err);
        if (lastbalance != balance || typeof lastbalance == 'undefined'){
            console.log('Last Balance:' + lastbalance);
            lastbalance = balance;  // JS will work its way back and the only last balance is in the 
                                    // window object, so it will use that, no need to pass it a 
                                    // reference, just address it globablly. 

            updateCauses();
         }
         console.log('Balance:', balance);
         if (typeof callback=='function') callback();
    });
};

setInterval(getBalance, 2000);