Last active
January 4, 2018 11:04
-
-
Save wiyoe/9a6869b045d32774bf876156146dd331 to your computer and use it in GitHub Desktop.
Javascript callback example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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