title: accessing outer scope in jQuery callback
body: <p>I have a situation where I need a jQuery callback to work on a variable outside of it's scope. For the sake of simplicity, assume the following code:</p>
  
  <pre><code>$('#myBtn').on('click', function(e) {
      var num = 1;
  
      // assume this returns the following:
      // { success : true, num : 1 }
      $.getJSON('/api/get_number', {}, function(response) {
          if ( response.success ) {
              // here, num is unknown unless I define it as a
              // global variable
              num += response.num ;   
          }
      });
  
      console.log(num); // =&gt; want 2, get 1
  });
  </code></pre>
  
  <p>I would like num to be available inside the callback function. What's a good way to do this without declaring global variables (declaring num outside the onclick function)? Is there a way to pass it as an argument without having to send it from the server? thanks.</p>