This article is a very lucid explanation of creating jQuery plugins. It explains the importance of $.fn and shows the use of $.extend and $.isFunction well. Obviously one can look at the source of jQuery but the author of the article has done a good job. The key points Are:
Wrapping your plug-in in (function( $ ){ // plugin goes here })( jQuery ); is desirable.
Don’t redundantly wrap the this keyword in the immediate scope of your plug-in’s function. Pay attention to this and $(this) in the article.
Payattaention to object chaining aspect by returning this. Unless you’re returning an intrinsic value from your plug-in, always have your plug-in’s function return the this keyword to maintain chainability.
Pass your plug-in settings in an object literal that can be extended over the plug-in’s defaults. Look at $.extend in the article.
Don’t clutter the jQuery.fn object (incidentally pronounced “jQuery effin”) with more than one namespace per plug-in.
Always namespace your methods, events, and data.
This good practice should get you going.
Thursday, July 25, 2013
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment