I was just going through Chris Coyer's examples of custom events, and came across the following code:
$.fn.faq = function(options) {
return this.each(function(i, el) {
var base = el,
$base = $(el);
console.log(options);
base.init = function() {
// Do initialization stuff
$base
.find("dd")
.hide()
.end()
.find("dt")
.click(function() {
var ans = $(this).next();
if (ans.is(":visible")) {
base.closeQ(ans);
} else {
base.openQ(ans);
}
})
};
base.openQ = function(ans) {
// Open panel
ans.show();
// Do callback
options.qOpen.call();
};
base.closeQ = function(ans) {
// Open panel
ans.hide();
// Do callback
options.qClose.call();
};
base.init();
});
};
$("dl").faq({
qOpen: myQuestionOpenCallback,
qClose: myQuestionCloseCallback
});
function myQuestionOpenCallback() {
alert("answer opened!");
}
function myQuestionCloseCallback() {
alert("answer closed!");
}
I am refering to Chris Coyer's post:
Now my question is why is in this code the JavaScript call() is obviously not being used to set the value of this, so why is call being used? Is it a good JS practice ? or is it just a author choice, because if I take off call on both the below lines of code I.E. :
options.qOpen.call();
options.qClose.call();
if changed to
options.qOpen();
options.qClose();
My plugin still works fine, so why the use of call()?
I am new to JS and jQuery.