That is half correct.

Obj.method1().method2().method3()

method1 return the modified original object, not a brand-new object.

You could do this:

var obj = $('#elementid');
obj.method1();
obj.method2();
obj.method3();

And it would be the same (with the same performance) of:

$('#elementid').method1().method2().method3();

Personally, I like chaining because it makes for tighter code.  You can also
do something like this:

$('#elementid').addClass('someclass')
    .children('div')
        .addClass('someclass')
    .end()
    .find('ul')
        .show();

That will find the element, add the class, then find the child divs and add
the class, then revert to the original element (#elementid) and then find
any ULs and show them.

JK

-----Original Message-----
From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of SLR
Sent: Wednesday, December 03, 2008 12:21 PM
To: jQuery (English)
Subject: [jQuery] Re: How beneficial is chaining methods?"


> > > One advantage to doing this
>
> > > $("#Results").html("Some Text").show();
>
> > > over this
>
> > > $("#Results").html("Some Text");
> > > $("#Results").show();
>
> > > would be that the script doesn't have to retrieve that wrapped set a
> > > second time

That's a good point. In this case, chaining would reduce overhead.






> > A chainable method, in jQuery, is written:
>
> > $.fn.newMethod = function() {
> >    // Function body...
> >    return $(this);
> > }
>
> > As you can see, all that's happening is "this" is being converted to a
> > jQuery object (defined by jQuery and its alias "$") and returned.

> Just a quick clarification on this. The this keyword within the
"newMethod"
> plugin you just made is already the jQuery object. All you need to do is
> return this;

So if I understand this correctly, essentially the line is execute
from left to right and returns the current object after each method
completes?

For Example:

Obj.method1().method2().method3()

This would do the following:
1)   Calls method 1 for the orignal Obj
2)   Calls method 2  for the obj that is returned from method 1
3)   Calls method 3 for the obj returned from method 2
4)   etc...

Is this correct?

Reply via email to