Skip to content

Instantly share code, notes, and snippets.

@stujo
Last active August 29, 2015 14:07
Show Gist options
  • Save stujo/e6ac79c6dd2ed5ece316 to your computer and use it in GitHub Desktop.
Save stujo/e6ac79c6dd2ed5ece316 to your computer and use it in GitHub Desktop.
JavaScript Event Driven Programming

#Event Driven Programming

#Overview

#First-class nature of functions

  • Functions are Objects
  • May be Anonymous
  • Assignable
  • Passable
  • Callable
// Function Declration
function bar(x){
  console.log('bar be done with ' + x + '!');
}
bar(7);
//=> bar be done with 7!

// Anonymous Function Expression Assigned to a variable
var foo = function(x){
  console.log('foo be done with ' + x + '!');
};
foo(8);
//=> foo be done with 8!

// Assign Function to Variable
var zee = bar;
zee(9);
//=> bar be done with 9!

function callMeBackLater(callback){
    callback(5);
}

callMeBackLater(foo);
//=> foo be done with 5!

#Callbacks and The JavaScript Event Loop

  • JavaScript is Single Threaded
  • Write short functions which execute quickly
  • Blocking operations are handled asynchronously and the results are processed by the callbacks we provide
    • mouse click
    • mouse move
    • key up
    • page loaded
    • ajax request response
    • timers - setTimeout and setInterval
  • clicker demo
    • onload
    • onclick
  • JavaScript handles this with an Event Loop
  • http://vimeo.com/96425312
  • Optional:
  • Optional:

#Review

  • Functions are Objects
  • Functions can be anonymous
  • Callbacks are
    • assigned to events
    • passed to blocking operations
  • JavaScript is Single Threaded
  • JavaScript has an Event Loop
  • jsbin and devtools t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment