title: Javascript: object return itself a.k.a. chaining
body: <p>I am <strong>trying to</strong> build my own little jquery-like library but I'm having a really rough time with creating this chaining pattern. Basically I have one class with a bunch of methods that make it easier to manipulate the document. Here's an example</p>
  
  <pre><code>function MF(selector){
      var DO; // Stands for DocumentObject
      this.select = function(selector){
          return document.getElementById(selector);
      }
      if(typeof selector === 'string'){
          DO = this.select(selector);
      }else if(selector instanceof HTMLElement){
          DO = selector;
      }
  
      this.children = function children(selector){
          return DO.getElementsByClassName(selector);
      }
      return {
          MF: ???
      }
  }(null);
  </code></pre>
  
  <p>I might be wrong in my reflections but what I've come to figure out is that in order to have additional methods for a document object ( html element ) I either need to extend the HTMLElement prototype or pass the element along with my class. I've chosen the second option. I just can't figure out what to return in my class so that  I can have chaining going on. What I simply aim at, for the sake of this example, is to be able to write the following line of code:</p>
  
  <pre><code>MF('someDiv').children('someClass');
  </code></pre>
  
  <p>In a desperate attempt I tried returning a new instance of <code>MF</code> which should not have instances by default and led myself to an infinite loop. I really cannot figure what I'm supposed to return there. Any help is greatly appreciated!</p>