views:

154

answers:

1

I want to execute method: bar() when method foo() is done.

This could be done like this:

function foo() {
  //....
  bar();
}

But there is surely some way to execute bar() like a callback to foo()? I have been looking in prototype API, but so far I only found ways to bind callbacks to HTML-elements...

+9  A: 
function foo(callback) {
   // ...
   callback();
}

And then you call foo like this:

foo(bar);
AgileJon
you might also want to check typeof callback to ensure it's a function
Russ Cam
Thanks, whats the syntax when doing this inside of class created with Class.create();
Andersson
Or another way - inside of function foo, assign callback = callback || function() {}; that way if callback is null or undefined, an empty function will be assigned and executed instead.
Russ Cam