Even more than that, if you were to return $(this) you wouldn't be chaining
on the original object.
 
    // Return the same object that this function was
    // called as a method of (i.e. the jQuery object).
    return this;
 
    // Return a *new* jQuery object that contains the
    // same DOM elements as this (probably not what
    // you want).
    return $(this);
 
Thomas, you're right that there's nothing new about chaining. Sometimes
people think that jQuery introduced the technique, but it's been common
practice in JavaScript and other languages for a long time. For example:
 
    var s1 = 'Testing 123';
    var s2 = s1.replace( '123', '456' ).toUpperCase();
 
-Mike


  _____  

From: Brandon Aaron

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;


--
Brandon Aaron


On Wed, Dec 3, 2008 at 1:01 PM, 703designs <[EMAIL PROTECTED]> wrote:



There's nothing special about chaining methods. You can do it in most
decent languages (in PHP, you could design methods to allow something
like: $toys->addNew("Block")->delete();) and all it involves is
returning an instance of the current object. It's not a performance
hit by any means.

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.

Thomas

Reply via email to