I look at the article "Creating a Plugin" at http://docs.jquery.com/Plugins/Authoring and saw this example in the "Defaults and Options" section:
$.fn.tooltip = function( options ) {
var settings = {
'location' : 'top',
'background-color' : 'blue'
};
return this.each(function() {
if ( options ) {
$.extend( settings, options );
}
I do not quite understand why the .extend call is inside the this.each block? My first intuition would be to write it as:
$.fn.tooltip = function( options ) {
var settings = {
'location' : 'top',
'background-color' : 'blue'
};
$.extend( settings, options || {} );
return this.each(function() {
So, I moved the .extend call over the return (and replaced the ugly if with || {} - I assume this makes sense?).
I see nothing here? Maybe some weird closure? How to: Change settings around the world and only on this challenge? (Edit: Apparently I'm not - http://jsfiddle.net/fVSDH/ )
source
share