Skip to content

Instantly share code, notes, and snippets.

@wiyoe
Last active January 4, 2018 11:04
Show Gist options
  • Select an option

  • Save wiyoe/9a6869b045d32774bf876156146dd331 to your computer and use it in GitHub Desktop.

Select an option

Save wiyoe/9a6869b045d32774bf876156146dd331 to your computer and use it in GitHub Desktop.
Javascript callback example
function Thing(name) {
this.name = name;
}
Thing.prototype.doSomething = function(callback, salutation) {
// Call our callback, but using our own instance as the context
callback.call(this, salutation);
}
function foo(salutation) {
alert(salutation + " " + this.name);
}
var t = new Thing('Joe');
t.doSomething(foo, 'Hi'); // Alerts "Hi Joe" via `foo`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment