views:

42

answers:

1

I'm curious, what are the (performance) differences in the ways that you can write a jQuery plugin, if any?

I've seen it done a couple of ways:

1. Using $.extend():

(function($){
  $.fn.extend({
    newPlugin: function(){
      return this.each(function(){

      });
    }
  });
})(jQuery);

2. Your own function:

(function($){
  $.fn.newPlugin = function(){
      return this.each(function(){

      });
  }
})(jQuery);

IMHO the second way is a bit cleaner and easier to work with, but seems like there could be some advantage to writing it with $.extend()? Or am I over-thinking this, there is no discernible difference and it's just a matter of personal preference?

(I would have thought this would have been asked before, but I can't find it -- if it's out there, please direct me to it)

+1  A: 

well, consider that when you do $.extend you're calling this method:

jQuery.extend = jQuery.fn.extend = function() {
    // copy reference to target object
    var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options, name, src, copy;

    // Handle a deep copy situation
    if ( typeof target === "boolean" ) {
        deep = target;
        target = arguments[1] || {};
        // skip the boolean and the target
        i = 2;
    }

    // Handle case when target is a string or something (possible in deep copy)
    if ( typeof target !== "object" && !jQuery.isFunction(target) ) {
        target = {};
    }

    // extend jQuery itself if only one argument is passed
    if ( length === i ) {
        target = this;
        --i;
    }

    for ( ; i < length; i++ ) {
        // Only deal with non-null/undefined values
        if ( (options = arguments[ i ]) != null ) {
            // Extend the base object
            for ( name in options ) {
                src = target[ name ];
                copy = options[ name ];

                // Prevent never-ending loop
                if ( target === copy ) {
                    continue;
                }

                // Recurse if we're merging object literal values or arrays
                if ( deep && copy && ( jQuery.isPlainObject(copy) || jQuery.isArray(copy) ) ) {
                    var clone = src && ( jQuery.isPlainObject(src) || jQuery.isArray(src) ) ? src
                        : jQuery.isArray(copy) ? [] : {};

                    // Never move original objects, clone them
                    target[ name ] = jQuery.extend( deep, clone, copy );

                // Don't bring in undefined values
                } else if ( copy !== undefined ) {
                    target[ name ] = copy;
                }
            }
        }
    }

    // Return the modified object
    return target;
};

but when you're calling $.fn.newPlugin, you're simply appending (or possibly overwriting) data on jQuery's prototype object, which is pretty much the exact same thing that $.fn.extend is doing anyways...

seems pretty self-explanatory which method has less overhead (the latter).

Dan Beam
This is what I suspected. Is there any advantage to writing one using the $.extend method?
munch
As far as I know, no, $.extend is useful to merge two objects together, but when you only have 1 "key" you're putting into and { object }, you're a) guaranteed to overwrite it if you put it later, and b) not doing anything else useful (like merging the keys from the other object. again, though, up to your preference (but I'd do it the second way).
Dan Beam