title: Create a reference to a method with parameters that uses the same scope - in Javascript
body: <p>I got some methods (<code>methA</code>, <code>methB</code> ...) that need to call the same method <code>methMain</code> in Javascript. This method <code>methMain</code> then need to fetch some data and when it is done do a callback to the method that called it (methA or MethB ...).</p>
  
  <p>I can successfully create a pointer/reference to a method by using what is written here: <a href="https://stackoverflow.com/questions/373157/how-can-i-pass-a-reference-to-a-function-with-parameters">How can I pass a reference to a function, with parameters?</a></p>
  
  <p>That solution, and all others I have seen, does not seem to work in the current scope.
  This code will not work:</p>
  
  <pre><code>function TestStructure() {
  
      this.gotData = false;
  
      //define functions
      this.methA = function (parA) { };
      this.methB = function (parb) { };
      this.createFunctionPointer = function (func) { };
  
  
      this.createFunctionPointer = function (func /*, 0..n args */) {
          var args = Array.prototype.slice.call(arguments, 1);
          return function () {
              var allArguments = args.concat(Array.prototype.slice.call(arguments));
              return func.apply(this, allArguments);
          };
      };
  
      this.methA = function (parA) {
  
          alert('gotData: ' + this.gotData + ', parA: ' + parA);
  
          if (this.gotData == false) {
              var fp = this.createFunctionPointer(this.methA, parA);
              this.methMain(fp);
              return;
          }
  
          //...do stuff with data
      }
  
      this.methB = function (parB) {
  
          alert('gotData: ' + this.gotData + ', parB: ' + parB);
  
          if (this.gotData == false) {
              var fp = this.createFunctionPointer(this.methB, parB);
              this.methMain(fp);
              return;
          }
  
          //...do stuff with data
      }
  
      this.methMain = function (func) {
  
          //...get some data with ajax
  
          this.gotData = true;
  
          //callback to function passed in as parameter
          func();
      }
  }
  
  var t = new TestStructure();
  t.methA('test');
  </code></pre>
  
  <p>When <code>methMain</code> do a callback to func (<code>methA</code> or <code>methB</code>) the variable <code>this.gotData</code> will not be set.</p>
  
  <p>Is there a solution for this problem or do I need to re-think the design?
  I want to do this to get data with ajax without blocking with async: false.</p>