refactor using js closure

Hi,

Nice page about [closure](http://stackoverflow.com/questions/12930272/javascript-closures-vs-anonymous-functions).  That means we can make this change.  I figured it would be a bit rude to push changes to master so I've made a pull request you can merge when your ready.  
The tests pass my end.