> When writing plugins, in general, when should I use jquery functions over
> standard javascript? (Sorry that question expose my ignorance.)
>
> I wondering when I should use a this.each in favor of a standard loop
> statement, or when to use getElementById instead of the dollar function?
>
> My worry is that referencing these functions has some overhead costs, and so
> wondering best practices.


That's a good question, Anaurag.  There's nothing wrong with using
plain-old-javascript in a plugin.  Just remember what jQuery is good
at.  If you're plugin does a lot of DOM selection for example, that
plays right into the strength of jQuery.  In general, jQuery is quite
fast, but don't be wasteful with the jQuery object.  For example,
avoid code that repeatedly creates a jQuery object like this:

jQuery.fn.myPlugin = function(options) {
    return this.each(function() {
        $(this).func1();
        ... // some processing
        $(this).func2();
        ... // some more processing
        $(this).func3();
    }
}

in favor of code that caches and reuses the jQuery object:

jQuery.fn.myPlugin = function(options) {
    return this.each(function() {
        var $this = $(this);
        $this.func1();
        ... // some processing
        $this.func2();
        ... // some more processing
        $this.func3();
    }
}

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to