简体   繁体   English

Node.js全局变量可访问性

[英]Node.js global variable accessibility

I am trying to write a NodeJS based application for which I declare a global variable and try to modify it in one of the functions in my code. 我试图编写一个基于NodeJS的应用程序,为此我声明一个全局变量,并尝试在我的代码中的一个函数中对其进行修改。 But it does not get modified. 但它不会被修改。 The code looks something like this: 代码看起来像这样:

var num=0, check = true;
function( a, b){
  var parts = a.split("/");
  while (num == 0){
    check=false;
    fileSystem.readFile(parts[parts.length-1], function (err, fileData) {  .
    .
    check= true
    }
 }
 if(check == true){
   num++;
   console.log("check: "+check+"num"+num);
 }
}

I see value of num as 0 always. 我总是看到num的值为0。 I also tried to use global.num inside the function but it dint work. 我也尝试在函数内部使用global.num,但它可以工作。 Please help. 请帮忙。 Thanks in advance. 提前致谢。

您的函数尚未被调用,因此没有理由更改num的值。

Even if your function were to be invoked, it contains an infinite loop (as far as I can tell, since you seem to have omitted some code in the loop). 即使要调用您的函数,它也会包含一个无限循环(据我所知,因为您似乎在循环中省略了一些代码)。 It will loop forever because it tests if num is 0, but num is only changed after the loop exits. 因为它将测试num是否为0,所以它将永远循环,但是num循环退出后才更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM