简体   繁体   中英

Variable is always logged as NaN

Can someone please explain to me why my var clients always returns NaN each time I try to call it with console.log .

//Imports
var inherits = require('util').inherits,
  MultiServer = require('../classes/servers/multi_server.js');
var clients = 0;

function PingServer(servers) {
  MultiServer.call(this, servers); //Inheritance

  //Basic server setup
  this.on('listen', function(params) {
    console.log("TCP server listening at", params['TCP'].ip + ":" + params['TCP'].port);
    console.log("Socket.IO server listening at", params['Socket.IO'].ip + ":" + params['Socket.IO'].port);
    console.log("All systems operational.");
  });

  this.on('error', function(err, source) {
    console.log("Error in " + source + " server:", err.message);
  });

  //Server behaviour
  this.on('connect', function(client) {
    console.log("A client has connected.");
    var clients = clients + 1;
    console.log("Client = " + clients);

    //Basic client setup
    client.on('disconnect', function() {
      console.log("A client has disconnected.");
      var clients = clients - 1;
      console.log("Clients = " + clients);
    });

    client.on('error', function(err) {
      console.log("A client has experienced an error:", err.message);
    });

    //Main server behaviour
    client.on('message', function(msg) {
      client.send(msg); //Ping reply
      console.log("Received message:", msg);
    });
  });
}
inherits(PingServer, MultiServer); //Inheritance
//Exports
module.exports = PingServer;

Varibles declared with var are function-scoped and hoisted in JavaScript. Everytime you type var clients it creates a variable in the scope of the current function.

This statement:

var clients = 0;

first declares the variable clients and then assigns it the value 0 .

Then you're writing

(function(){
  var clients = clients + 1;
  console.log("Client = " + clients);
})

However, this does the same thing again : it declares a new variable clients and scopes it to the function. This new clients variable is distinct from the initial one. In this function it is shadowed (ie it cannot be accessed). Effectively, clients is undefined in the function.

Hoisting means that variable declarations are moved to the top of the function automatically.

That means the statement

var clients = clients + 1;

in the function is equivalent to

var clients = undefined + 1;

which is

var clients = NaN;

If, however, you removed the var keyword, it wouldn't create a new variable but instead use the old one :

var clients = 0;

(function(){
  clients = clients + 1; // clients = 0 + 1 === 1;
  console.log("Client = " + clients); // "Client = 1"
})

And clients = clients + 1 is probably better written as clients++ .


You have multiple declarations for clients variable.

change this

var clients = clients + 1;

to

clients = clients + 1;

& this

var clients = clients - 1;

to

clients = clients - 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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