.each() is fine if you want to iterate through the set.

I'm proposing something different, the ability to work on the set as a
whole in the same way that a plugin would,
without having to break out of the chain. Example:


var that = $(selector)
    .doSomething();

if (test) that.doSomethingOptionally();

that.doMoreStuff();


Instead we could do:


$(selector)
    .doSomething()
    .fn(function() {
        if (test) this.doSomethingOptionally();
        return this;
    })
    .doMoreStuff();


Or, for debugging:

$(selector)
    .doSomething()
    .fn(function() { console.log(this.get()); return this; })
    .doMoreStuff();


But the primary use is to make task specific plugins or reusable
chains without unnecessary pollution of the $.fn namespace. Example:

function addMyStuff() {
     return this.addClass('mine').removeClass('theirs').css
('position', 'absolute');
}

Then at any time in my code I just need to do:

$(selector).fn(addMyStuff);

Maybe 'fn' is not a great name. I would like to call it 'apply' but
that's taken ;(

Regards
- Mark

On Dec 18, 3:27 pm, "John Resig" <[email protected]> wrote:
> How much does this differ from using .each?
>
> $(selector).each(function() {
>     return $(this).each(myfn)  // A function that adds children
>            .children()
>                .each(arguments.callee)  // Do the same to the children
>            .end();
>
> });
>
> and:
>
> $(selector).each(myfn,['test']).each(myfn2).each(myfn3);
>
> The only significant difference that I see is that .each requires that
> you wrap the 'this' with $(...) - but I think that's a fair price to
> pay for having API compatibility (your proposed method would be the
> only jQuery method to not have its 'this' be a DOM node).
>
> --John
>
> On Thu, Dec 18, 2008 at 10:06 AM, Mark Gibson <[email protected]> wrote:
>
> > Hi, I don't know if this has been suggested before, I couldn't find
> > anything in this mailing list.
>
> > I sometimes find it handy to write reusable functions as if they were
> > $.fn plugins.
> > Functions that may be used elsewhere but aren't general enough to
> > actually put in $.fn.
>
> > Example:
>
> > function myfn = function(v) {
> >   return this.filter(...).each(function() { ...use arg v for
> > something... }).end();
> > };
>
> > Called like this:
>
> > myfn.call($(selector), 'test').show();
>
> > From this I can chain other jQuery functions, but not another similar
> > style call.
>
> > I propose a simple addition to jQuery:
>
> > // Apply a function to a jQuery object, as if it were a plugin
> > $.fn.fn = function(fn) {
> >        return fn.apply(this, Array.prototype.slice.call(arguments,1));
> > };
>
> > So instead, the call becomes:
>
> > $(selector).fn(myfn,'test').show();
>
> > and chaining is nicer:
>
> > $(selector).fn(myfn,'test').fn(myfn2).fn(myfn3);
>
> > It also allows inline recursive functions (useful for building trees):
>
> > $(selector).fn(function() {
> >    if (this.length) {
> >        this.fn(myfn)  // A function that adds children
> >            .children()
> >                .fn(arguments.callee)  // Do the same to the children
> >            .end();
> >    }
> >    return this;
> > });
>
> > I know it could just be a plugin, but it is very small and seems
> > useful enough to add into the core,
> > and it could encourage developers to program more in the plugin style.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to