title: Pass global variable to on event callback function in Javascript?
body: <p>In the following code, I'm trying to get <code>mydata</code> passed to the callback. This is a sample problem from a bigger problem I'm having. I'm assuming this is a scope issue. What is wrong with my code and/or what should I do?</p>
  
  <pre><code>var EventEmitter = require('events').EventEmitter;
  var myevent      = new EventEmitter();
  var mydata       = 'my data!';
  
  function myfunc (data, mydata) {
    console.log(data);
    console.log(mydata);
  };
  
  myevent.on('data', function(data, mydata) {myfunc(data,mydata)});
  
  myevent.emit('data', "data!");
  </code></pre>
  
  <p>returns:</p>
  
  <pre><code>data!
  undefined
  </code></pre>
  
  <p>I'd like it to return:</p>
  
  <pre><code>data!
  my data!
  </code></pre>