Skip to content

Instantly share code, notes, and snippets.

@christianbundy
Last active August 29, 2015 13:58
Show Gist options
  • Save christianbundy/10225380 to your computer and use it in GitHub Desktop.
Save christianbundy/10225380 to your computer and use it in GitHub Desktop.
// THE WRONG WAY TO DO IT ////////////////
_([1, 2, 3]).forEach(function(num) { // for numbers 1..3, create a function that takes a `num`
$('div.' + num).click(function () { // when a div with the class `num` is clicked
console.log('you clicked ' + num); // log "you clicked `num`"
}); //
}); //
//////////////////////////////////////////
// THE RIGHT WAY TO DO IT //////////////
var bindToClick = function (num) { // create a function that takes a `num`
$('div.' + num).click({ // when a div with the class `num` is clicked
"num": num // set event.data.num to `num`
}, logClick); // call our logClick function, passing it `event` implicitly
}; //
//
var logClick = function (event) { // create a function that handles the `event` object
var num = event.data.num; // grab number from event.data.num
console.log('you clicked ' + num); // log "you clicked `num`
}; //
//
_([1, 2, 3]).forEach(bindToClick); // for numbers 1..3, call our function
////////////////////////////////////////
@TahirAhmed
Copy link

I see that now and understand where you are coming from. But may be I wasn't able to lay down what exactly I meant by "performance" and what approach I would suggest in such a case. Perhaps I should find some time to make an example script to tell you what I mean.

Thanks a lot for the explanations though. Appreciate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment