I have a small question regarding scoping when i use a custom callback function passed to jQuery.animate(). Consider the code below.
;var WebApi = (function projectInit (WebApi, $, Modernizr, window, document, undefined) {
$(element).on('click.skiptodocumentpart', function skipToDocumentPart (e) {
WebApi.scrollTo(
$(this.getAttribute('href')).offset().top,
1000,
$pageNav.length ? repositionPageNav($pageNav, $(this)) : null
);
return false;
});
function repositionPageNav ($pageNav, $elm) {
console.log(this); // undefined
};
WebApi.scrollTo = function (yPos, iSpeed, callback) {
$('html,body').animate({
scrollTop : yPos
},{
duration : typeof iSpeed === 'number' ? iSpeed : 1000,
complete : typeof callback === 'function' ? callback : null
});
};
return WebApi;
}(WebApi || {}, jQuery, Modernizr, this, this.document));
When i log the function context in the repositionPageNav callback i get a undefined, ideally i would have the context reference the element clicked on, as jQuery itself ussualy do. I could do this by changing the callback call to WebApi.scrollTo as follows:
WebApi.scrollTo(
yPos,
1000,
as$pageNav.length ? (function (scope) {
return repositionPageNav.call(scope, $pageNav);
}(this)) : null
);
By creating the closure the context inside the callback now refers to the element clicked as i intended but does this hurt performance? And 1 thing i cannot get my head around is why in the first example the context is undefined? Is it because i don't call the callback as callback.call(context)? Any suggestions/ideas?
Many Thanks,
Nick.