>>> I am trying to figure out how jQuery works internally. There is a 
>>> stack maintained within jQuery but why does jQuery change the 'this' 
>>> array of objects? why not just pass back the modified array when 
>>> using things like find?
>>
>> The stack allows to revert to older states. Check the documentation 
>> for end(), that explains it better.
>
> Yeh, I got that, but when you try to use a jquery object twice in a 
> chain it doens't work...
>
>  $('div').each(
>    $('div').find('img').hide();
>    $('div').find('img').show();
>  );
> Wouldn't it just be easier to pass back the this (instead of changing
> in the jquery object itself)?

Well, that makes three votes this week.

Right now, jQuery methods change the original object and return that or
chaining.

If you wanted to experiment with returning a fresh jQuery object rather than
changing the original, and I think John is considering it, it seems like the
change would be localized to pushStack. I have no idea what the other
implications are, but this passes the test suite. Did I get this right?

        pushStack: function(a,args) {
                var fn = args && args[args.length-1];
                var fn2 = args && args[args.length-2];

                if ( fn && fn.constructor != Function ) fn = null;
                if ( fn2 && fn2.constructor != Function ) fn2 = null;

                if ( !fn ) {
                        var fresh = new jQuery(this);
                        fresh.stack = [ this.get() ];
                        fresh.get( a );
                        return fresh;
                }
                        
                // function(s) specified, stay with existing object
                var old = this.get();
                this.get( a );  // just temporary
                if ( fn2 && a.length || !fn2 )
                        this.each( fn2 || fn ).get( old );
                else
                        this.get( old ).each( fn );
                return this;
        }


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to