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.
Once 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
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.
I'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.
These 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'.
18 comments
[ 4.5 ms ] story [ 62.3 ms ] threadActually 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.
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!
Thanks
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.
Isn't a little bit inconvinient, talking about modularity, will you be the only one using those plugins?
https://github.com/twbs/bootstrap/issues/3057
Edit: I guess such a thing is not really a 'jQuery plugin' but a 'module that depends on jQuery'.