Skip to content

Instantly share code, notes, and snippets.

@hcmn
Created July 17, 2012 13:59
Show Gist options
  • Save hcmn/3129527 to your computer and use it in GitHub Desktop.
Save hcmn/3129527 to your computer and use it in GitHub Desktop.
JQuery: Callback example
function Thing(name) {
this.name = name;
}
Thing.prototype.doSomething = function(callback) {
// Call our callback, but using our own instance as the context
callback.apply(this, ['Hi', 3, 2, 1]);
}
function foo(salutation, three, two, one) {
alert(salutation + " " + this.name + " - " + three + " " + two + " " + one);
}
var t = new Thing('Joe');
t.doSomething(foo); // Alerts "Hi Joe - 3 2 1" via `foo`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment