Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created January 18, 2010 14:57
Show Gist options
  • Save cowboy/280072 to your computer and use it in GitHub Desktop.
Save cowboy/280072 to your computer and use it in GitHub Desktop.
jQuery event handler context examples
var foo = {
context: 'obj',
func: function(){
console.log( this.context );
}
};
var bar = foo.func;
window.context = 'global';
document.context = 'document';
foo.func(); // logs: 'obj'
bar(); // logs: 'global'
$(function(){ foo.func(); }); // logs: 'obj'
$(function(){ bar(); }); // logs: 'global'
$( foo.func ); // logs: 'document'
$( bar ); // logs: 'document'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment