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.
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
Post a Comment