JS Callbacks
by Ryan Morris
JavaScript
// callbacks
var starterFunction = function (callback) {
// do something first
return callback();
};
// verify callback is a function
function getInput(options, callback) {
// do some things
if (typeof callback === "function") {
callback(options);
};
};
// handling scope issues in callbacks
function Thing(name) {
this.name = name;
};
Thing.prototype.doSomething = function (callback, arg1) {
// call the callback using our own scope
callback.call(this, arg1);
};
function foo() {
alert(this.name);
};
var t = new Thing('Joe');
t.doSomething(foo);