簡體   English   中英

需要在一個函數外部和另一個內部訪問變量

[英]Need to access variable outside a function and inside another

我有一個問題,我想從FeederBot函數內部訪問data.names,以便我可以訪問從客戶端發出的名稱。 但是我不知道該怎么做。 我希望在feeder bot中訪問this.nickname = data.names的變量。 有什么幫助嗎?

io.on('connection', function(socket) {

    socket.on('login', function(data) {
        console.log("User connected with id:" + data.uuid + " Bot names: " + data.names);
        socket.room = data.uuid;
        socket.join(data.uuid);

        if (data.type == "server") {
            io.sockets.in(socket.room).emit("force-login", "server-booted-up");
        }
    }.bind(this));

    socket.on('pos', function(data) {
        //console.log(socket.room + " : " + data);
        io.sockets.in(socket.room).emit('pos', data);
    });

    socket.on('cmd', function(data) {
        console.log(data);
        io.sockets.in(socket.room).emit('cmd', data);
    });

    socket.on("spawn-count", function(data) {
        io.sockets.in(socket.room).emit("spawn-count", data);
    });

    socket.emit("force-login", "startup");

});

function FeederBot(bot_id, agent, bot_number, server) {

    this.bot_id = bot_id; //ID of bot for logging
    this.interval_id = 0; //here we will store setInterval's ID
    this.ball_id = null;
    this.server = ''; //server address will be stored here
    this.client = new AgarioClient('Bot_' + this.bot_id); //creates new client
    this.client.debug = 0;
    this.client.agent = agent;
    this.client.headers['user-agent'] = config.userAgent;
    if (facebookManager.hasAvailableToken()) {
        this.client.auth_token = facebookManager.getToken();
    }
    this.isOnFeedMission = false; this.nickname = "Exper"

    this.lastsent = {minx: 0, miny: 0, maxx: 0, maxy: 0};
    this.onboard_client(server, bot_number);
}
var outsideVar = null;

function functionOne() {
  outsideVar = 1;
}

function functionTwo() {
  console.log(outsideVar);
}

functionOne(); // set the value of your var
functionTwo(); // output in console should be 1

只需看看定義var的范圍即可。 在這種情況下,js的結構是結構化的,您可以訪問在定義函數之前定義的變量,並且可以在與定義函數相同的作用域級別上訪問

編輯:為了更好的理解

var dataStorage = null; // before you enter any { scope with socket

{
  // ...
  socket.on('login', function(data) {

    dataStorage = data; // <<- write data to the global var dataStorage

    console.log("User connected with id:" + data.uuid + " Bot names: " +        data.names);
    socket.room = data.uuid;
    socket.join(data.uuid);

    if (data.type == "server") {      
      io.sockets.in(socket.room).emit("force-login", "server-booted-up");
    }

  }.bind(this));
  // ..
}

function FeederBot(bot_id, agent, bot_number, server) {

  this.bot_id = bot_id; //ID of bot for logging
  this.interval_id = 0; //here we will store setInterval's ID
  this.ball_id = null;
  this.server = ''; //server address will be stored here
  this.client = new AgarioClient('Bot_' + this.bot_id); //creates new client
  this.client.debug = 0;
  this.client.agent = agent;  
  this.client.headers['user-agent'] = config.userAgent;
  if (facebookManager.hasAvailableToken()) {
    this.client.auth_token = facebookManager.getToken();
  }
  this.isOnFeedMission = false; 
  this.nickname = "Exper"

  // and here you can access the global var again. 
  // make sure u test for not null and the property exists
  if(!!dataStorage) {
    if(dataStorage.hasOwnProperty('names')){
      this.nickname = data.names;
    }
  }

  this.lastsent = {minx: 0, miny: 0, maxx: 0, maxy: 0};
  this.onboard_client(server, bot_number);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM