Reputation: 19071
I'm learning jQuery and saw this chunk of code.
myJqueryFile.js
(function($, undefined) {
function Calendar(element, options, eventSources) {
var t = this;
t.incrementDate = incrementDate;
....some code here
function incrementDate(years) {
if (years !== undefined) {
addYears(date, years);
}
renderView();
}
}
})(jQuery);
In my html, I am referencing the js above externally and want to call incrementDate() but I'm keep getting "increment is not function".
But I think incrementDate() is not private function so I should be able to call it from outside. Is it really possible?
I'm calling incrementDate like below:
<a href="" onclick="incrementDate();" />
Oops, I totally missed the surrounding jQuery bracket!
Upvotes: 2
Views: 683
Reputation: 81660
Yes, you can call it. You might have to call it using the apply which allows you to define this
and arguments
.
There is some docs here as well.
If you need some code:
var result = Calendar.prototype.incrementDate.apply(mycalendarObj, myArguments);
OK, it seems all you need is:
calObject.incrementDate(1,1,1); // adds one year + one month + one day
Upvotes: 2
Reputation: 943510
You can only call it on an instance of Calendar.
var cal = new Calendar(element, options, eventSources);
cal.incrementDate();
And Calendar is scoped to anonymous function you wrap the whole thing is. So that code can only appear inside that function (unless you do something to expose it).
Upvotes: 1