user2592038
user2592038

Reputation: 375

Understanding of callback function in javascript

I have a basic javascript question. This code seems to call a function inside the same function createEvent. But the function itself doesn't have any parameters inside, so how can it call the same function createEvent inside createEvent with the parameters title, start, and end?

function createEvent(calendarId) {
  var cal = CalendarApp.getCalendarById(calendarId);
  var title = 'Script Demo Event';
  var start = new Date("April 1, 2012 08:00:00 PDT");
  var end = new Date("April 1, 2012 10:00:00 PDT");
  var desc = 'Created using Google Apps Script';
  var loc = 'Script Center';

  var event = cal.createEvent(title, start, end, {
      description : desc,
      location : loc
  });
};

Can anyone here help me explain this?

Upvotes: -1

Views: 69

Answers (1)

MattSenter
MattSenter

Reputation: 3120

Your top-level createEvent has no scope, i.e. it belongs to no object, and therefore, it is completely different from the scoped function createEvent that apparently exists on a Calendar object of some sort. In short, these are not the same functions. The "inner" one is actually a member of another object.

I should point out, however, that with javascript, you can pass as many arguments as you'd like to any function. Just because the prototype lists a single argument does not limit the number of arguments you can pass. That does not apply in this particular situation, but it's good for you to know for future reference.

Upvotes: 2

Related Questions