Skip to content

Instantly share code, notes, and snippets.

@stujo
Created November 4, 2014 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stujo/c6bd50cf18fb2e9c4b16 to your computer and use it in GitHub Desktop.
Save stujo/c6bd50cf18fb2e9c4b16 to your computer and use it in GitHub Desktop.
Event Driven Javascript

#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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment