Advertisement
thetenfold

Untitled

Jul 14th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function foo (event) {
  2.     // doesn't have a return statement
  3.     // so this function returns undefined
  4.     if (event) {
  5.         alert('foo() was invoked by an event listener.');
  6.     } else {
  7.         alert('foo() was invoked some other way.\n(e.g., while adding the event listener, to get its return value)');
  8.     }
  9. }
  10.  
  11. // this listener will run the foo function, get its return value, *then* attempt to add the listener
  12. window.addEventListener('click', foo(), false);
  13.  
  14. // since foo() doesn't return anything, it actually does this
  15. window.addEventListener('click', undefined, false);
  16. // foo() does get executed once, because you invoked it to get its return value
  17. // but it will not actually add a listener
  18.  
  19. // now let's add a real listener, and our foo() function will tell us some info whenever we call it
  20. window.addEventListener('click', foo, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement