JQuery plugin options: extension to or inside of this. block?

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 exist, lets merge them
  // with our default settings
  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/ )

+3
source share
1

.

settings jQuery.

|| {} , .

var settings = $.extend({
   'location':          'top',
   'background-color':  'blue'
}, options || {});

.

+3

Source: https://habr.com/ru/post/1771994/


All Articles