How to detect chained function calls in JavaScript?
<pre><code>var Obj = {
   func1 : function() {
      // some code
      if (this._hasChainedFunc()) {
         // block should be CALLED
      }
      return this;
   },
   func2 : function() {  
      // some code
      if (this._hasChainedFunc()) {
         // block should be NOT called
      }
      return this;
   },
   _hasChainedFunc : function() {
     // code which detects if there is a chained function???
   }
}

Obj.func1().func2();
</code></pre>

<p>Is there a possible implementation for function <strong>_hasChainedFunc()</strong>? This function should return <em>true</em> on the first call (because func2() is called afterwards), <em>false</em> on the second call.</p>

<p>In a more advanced version, <strong>_hasChainedFunc()</strong> may also returned the function which is actually called afterwards.</p>

<p><strong>UPDATE</strong></p>

<p>I created a lib which does what I want. It's available on GitHub and I wrote a little documentation <a href="http://mstumpp.github.com/chain.js/" rel="nofollow">here</a>. </p>

<p>In short: Take an arbitrary JavaScript function and "chainify" it:</p>

<pre><code>var Model = function() {};

Model.prototype.func1 = function() {
  console.log('func1 has ' + this.c_getPredecessors().length + ' preceding functions');
  return this.c_delay().c_chain(function() { 
    console.log('func1 has ' + this.c_getSuccessors().length + ' succeeding functions');     
    console.log('func1 processing...');
    this.c_next();
  });
};

Model.prototype.func2 = function() {
  console.log('func2 has ' + this.c_getPredecessors().length + ' preceding functions');
  return this.c_delay().c_chain(function() {
    console.log('func2 has ' + this.c_getSuccessors().length + ' succeeding functions');     
    console.log('func2 processing...');
    this.c_next();
  });
};
</code></pre>

<p><a href="https://github.com/MStumpp/chain.js" rel="nofollow">Chainify</a> and instantiate it, and call some functions:</p>

<pre><code>chainify(Model);

var Obj = new Model();
Obj.func1().func2();
</code></pre>

<p>Console output:</p>

<pre><code>func1 has 0 preceding functions
func2 has 1 preceding functions
func1 has 1 succeeding functions
func1 processing...
func2 has 0 succeeding functions
func2 processing... 
</code></pre>

<p>Of course, this is a simple example. It just demonstrates that every functions is now capable to access information about what happens before and after the current function call.</p>
