overriding of jQuery .end() method to accept parameters
<p><strong>UPDATE</strong>
the current solution lives on this gist in case you ever need this type of functionality
<a href="https://gist.github.com/jdaly13/5581538#file-gistfile1-js" rel="nofollow">https://gist.github.com/jdaly13/5581538#file-gistfile1-js</a></p>

<p>I'm trying a little experimentation where I'm trying to override jQuery's core end method with my own method that just essentially adds functionality to it</p>

<p>for example if you are working with jQuery you might do something like this</p>

<pre><code>$('h1.clickme').click(function () {
     $(this).next('div').css("width", "+=200").parent().next().hide().end().end()
      //this should traverse you back to the div after the h1 clicked
})
</code></pre>

<p>I want to override the jQuery end method by adding the ability to add a number parameter 
for example </p>

<pre><code>.end(1) 
</code></pre>

<p>would do the same as</p>

<pre><code>.end().end()
</code></pre>

<p>Here is what I have so far</p>

<pre><code>(function(){

// Define overriding method.
jQuery.fn.end = function(no_of_times){
    var prevObject = "prevObject";
    if (!(arguments.length) || (typeof no_of_times !== "number")) {
        return this.prevObject || this.constructor(null);
    } else {
        if (no_of_times == 1) {
            return this.prevObject.prevObject || this.constructor(null)
        } else if (no_of_times == 2) {
            return this.prevObject.prevObject.prevObject || this.constructor(null)  
        } else if (no_of_times == 3) {
            return this.prevObject.prevObject.prevObject.prevObject || this.constructor(null)
        } else {
            //too many times can't type anymore
            return this.prevObject || this.constructor(null);
        }           
    }

}
})()
</code></pre>

<p>so I could call something like this</p>

<pre><code>$('.promo-carousel').parent().prev().siblings('span').hide().end(2)
</code></pre>

<p>But obviously I don't think it's efficient to write out all those if statements
Is there an easier way to do this?  I'm sure the performance is about the same but I'm doing this more for readability sake </p>

<p>I thought of appending the prevObject variable with "prevObject" string based on the parameter passed something like this</p>

<pre><code>if (!(arguments.length) || (typeof no_of_times !== "number")) {
            return this.prevObject || this.constructor(null);
        } else {
            for (i=0; i &lt;= no_of_times; i++) {
                pObj += "prevObject."           
            }
            var prevObject = pObj.slice(0, prevObject.length -1)
            console.log(prevObject);
            return this.prevObject
        }

    }
</code></pre>

<p>But that snippet of code above didn't work
Any help is appreciated as always</p>
