javascript - 需要访问函数外部和另一个函数内部的变量

标签 javascript

我有一个问题,我想从 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

只需查看您定义变量的范围即可。在这种情况下,Node.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);

}

关于javascript - 需要访问函数外部和另一个函数内部的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42329260/

相关文章:

javascript - 如何用纯JS检查显示的是哪个背景图片

javascript - 带有动态模板的指令

javascript - 根据 JavaScript 数组中的条件拆分值

范围减少的Javascript数组

javascript - polymer 过滤器表达式的上下文

javascript - 如何更改 Three.js 中的相机类型?

javascript - REGEX - 当内部内容有换行符时 HTML 到简码中断

javascript - 我可以使 Bootstrap 可折叠面板仅在输入密码后折叠吗?

javascript - 在 Angular JS 中垂直对齐单选按钮

Javascript 使用 jQuery UI 自动完成电子邮件的域