Javascript 如何将其他参数传递给事件处理程序?

Javascript 如何将其他参数传递给事件处理程序?,javascript,node.js,Javascript,Node.js,我正在使用库以ftp方式获取/放置文件 // establish a ftp connection function connect(message) { client.connect(ftpProperties); } // once connection is successfully established, ready event will be emitted client.on('ready', function() { console.log('ftp connec

我正在使用库以ftp方式获取/放置文件

// establish a ftp connection
function connect(message) {
    client.connect(ftpProperties);
}


// once connection is successfully established, ready event will be emitted
client.on('ready', function() {
  console.log('ftp connection is success.');
  // do something
  // How to access message object here?
});
我尝试的内容:如果我在connect函数中使用ready事件处理程序 每次调用connect函数时都注册了

function connect(message) {
  client.once('ready', function() {
    // use message
  });
  client.connect(ftpProperties);
}

请建议我如何将消息对象从connect函数传递到ready event handler函数?

一种方法是在connect函数中注册一个单计时器事件侦听器

function connect(message) {
  client.once('ready', function() {
    // use message
  });
  client.connect(ftpProperties);
}

您可以将其保存在数组中,稍后再使用:

var myArray = new Array(); //create an array

// establish a ftp connection
function connect(message) {
    client.connect(ftpProperties);
    myArray.push(message);
}



// once connection is successfully established, ready event will be emitted
client.on('ready', function() {
  console.log('ftp connection is success.');

  console.log(myArray[0]); //use it here

}); 

我猜无论调用什么
connect
都可以访问
消息
,因此应该负责创建一个事件处理程序,该处理程序关闭
消息
,并可以传递给
客户端。on('ready')
,而不是您正在使用的事件处理程序。