5 ms·
My Favorite jQuery Plugin Template
- phlyingpenguin 13y agoWhy not: options = $.extend(true, {}, defaults, options);
- kolodny 13y agoBecause I didn't think of it :) Actually that's better because if one of the options is an object I wouldn't have to worry about accidentally changing a propery because this does a deep copy. I've updated the post, thanks.
- cturhan 13y agoWhat is the problem with `$this = $(this)` ? You used `that` but I didn't get that.
- kolodny 13y agoOnce I set up an alias for `this` I try to only use that from then on to be consistent. The reason I do that in the first place is in case I need to pass it to a callback, I can just use the alias and don't have to worry about when `this` will be the global object
- jasonlotito 13y agoIf you minimize your javascript, `that` can be minimized, `this` cannot.
- 5vforest 13y agoNeat. I also like this one (Coffeescript): https://gist.github.com/rjz/3610858 https://gist.github.com/rjz/3610858
- deleted 13y ago[deleted]
- rdebeasi 13y agoCool stuff! This reminds me a lot of jQuery Boilerplate: https://github.com/jquery-boilerplate/jquery-boilerplate/blob/master/src/jquery.boilerplate.js https://github.com/jquery-boilerplate/jquery-boilerplate/blo... Personally, I find the "that" variable to be a little confusing. A developer looking at the code might wonder whether "that" is the plugin, a DOM element, or something else. To make the code a little clearer, you could use something like "elem" to represent the element on which the plugin has been called, and "plugin" to represent the plugin itself. Happy coding!
- kolodny 13y agoThat's a good point. I've updated the post to use `elem` and `$elem` Thanks
- maemilius 13y agoI'm not a huge fan of the double wrap of anonymous functions. The inner one seems entirely superfluous unless you're adding multiple plugins in one file and, I think, makes the code a little harder to read since you have to look at the invocation of the function to get all the information you need about it. It seems a lot clearer to simply declare `var pluginName = "borderize";` at the top of the function than to pass it in. Granted, that does add 6 characters to the total length of the file.
- kolodny 13y agoWell I plan on using this for multiple plugins in that same iffe. I also like that I'm able to copy/paste it as a block inside other jQuery blocks.
- jbeja 13y ago"I plan on using this for multiple plugins in that same iffe" Isn't a little bit inconvinient, talking about modularity, will you be the only one using those plugins?
- xiata 13y agoDouble wrapping is necessary for closure compiler to collapse variable names.
- xiata 13y agoSave a few bytes. ;!function($){ ... return this.each(function($elem, elem){ $elem = $(elem); ... }); ... }(jQuery);
- prezjordan 13y agoHo boy not this again... https://github.com/twbs/bootstrap/issues/3057 https://github.com/twbs/bootstrap/issues/3057
- nilliams 13y agoThese days I'd prefer a plugin template that doesn't infect the jQuery namespace by default (and is AMD compatible) so I can use it with a module loader: var borderize = require('jquery.borderize'); ... borderize('div'); And my linter will tell me off if I forget a require. Edit: I guess such a thing is not really a 'jQuery plugin' but a 'module that depends on jQuery'.
- amadeus 13y agoHow does anyone consider this sane javascript? Is this what the community has been reduced too?
- voidr 13y agoI find the pluginName parameter excessive.