title: Javascript: Passing custom arguments to a callback function
body: <p>I have this callback function setup:</p>
  
  <pre><code>var contextMenu = [];
  var context = [ { "name": "name1", "url": "url1" }, {"name": name2", "url: "url2" } ];
  for(var i=0; i &lt; context.length; i++) {
      var c = context[i];
      var arr = {};
      arr[c.name] = function() { callback(c.url); }
      contextMenu.push( arr );
  }
  function callback(url) {
     alert(url);
  }
  </code></pre>
  
  <p>The problem is that the url value passed to the callback is always the last value in the context variable - in this case "url2". I am expecting to pass specific values to each "instance" of the callback, but as the callback seems to be remember the same value, the last time it was referred.</p>
  
  <p>I am kind of stuck. Any help would be appreciated.</p>
  
  <p>PS: I am using <a href="http://www.javascripttoolbox.com/lib/contextmenu/" rel="noreferrer">jQuery ContextMenu</a> which, to my understanding, does not support sending custom data to its callback functions. It is in this context that I have this problem. Any suggestions to overcome in this environment is also helpful!</p>