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
-~----------~----~----~----~------~----~------~--~---