Skip to content

Instantly share code, notes, and snippets.

@joshgillies
Created July 20, 2012 04:07
Show Gist options
  • Save joshgillies/3148613 to your computer and use it in GitHub Desktop.
Save joshgillies/3148613 to your computer and use it in GitHub Desktop.
Fun with JS callbacks!
// getting my head around callbacks, would like to take this further. Maybe another time.
// See this code live: http://jsconsole.com/?function%20testMe(%20a%2C%20b%2C%20c%2C%20callback%20)%20%7B%0A%20%20var%20test%20%3D%20a%2C%0A%20%20%20%20%20%20test2%20%3D%20b%2C%0A%20%20%20%20%20%20test3%20%3D%20c%3B%0A%20%20console.log(test%20%2B%20%22%20%22%20%2B%20test2%20%2B%20%22%20%22%20%2B%20test3)%3B%0A%20%20test2%20%3D%20b%20%2B%207%3B%0A%20%20return%20callback(%20test%2C%20test2%2C%20test3%20)%3B%0A%7D%0A%0Afunction%20testThis(%20callback%20)%20%7B%0A%20%20console.log(%22helloworld%22)%3B%0A%20%20console.log(typeof%20callback)%3B%0A%20%20(typeof%20callback%20%3D%3D%3D%20'number'%20%3F%20%20callback%20%3D%20callback%20%2B%207%20%3A%20callback)%3B%0A%20%20return%20callback%3B%0A%7D%0A%0AtestThis(%20testMe(%20%22string%22%2C%2020%2C%20false%2C%20function%20()%20%7B%0A%20%20return%20test2%3B%0A%7D))%3B
function testMe( a, b, c, callback ) {
var test = a,
test2 = b,
test3 = c;
console.log(test + " " + test2 + " " + test3);
test2 = b + 7;
return callback( test, test2, test3 );
}
function testThis( callback ) {
console.log("helloworld");
console.log(typeof callback);
(typeof callback === 'number' ? callback = callback + 7 : callback);
return callback;
}
testThis( testMe( "string", 20, false, function () {
return test2;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment