javascript - using this rather than $(this) in an .each() call -


i've done quite lot of reading past day deeper understanding of vs. $(this) , how js determines interprets, still can't figure out 1 detail of plugin i'm analyzing deepen knowledge:

$.fn.pluginname = function(options) {   return this.each(function() {     new $.pluginname(this,options);   }); }; 

everything i've read indicates although this.each() used call jquery.prototype.each(), each object should referred $(this) within each() function, above uses regular ol' this, , can't figure why. $.pluginname declaration looks this:

$.pluginname = function(el,options) {   ... } 

any insights may have big help.

edit: here's full source on github

from mdn:

when function called method of object, set object method called on.

when call like

$('#mydiv').pluginname() 

the function called method of jquery object $('#mydiv'). so, in case, this refers jquery object, don't need $() wrapper.

a common case when need $() when binding events.

$('#mydiv').on('click', function(){     alert('you clicked div!'); }); 

the difference here function called not on $('#mydiv'), dom element belongs to. so, here this dom object, not jquery object.


inside callback function passed each, this refers dom element, not jquery object.. $.pluginname wants. expects dom element.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -