Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function foo (event) {
- // doesn't have a return statement
- // so this function returns undefined
- if (event) {
- alert('foo() was invoked by an event listener.');
- } else {
- alert('foo() was invoked some other way.\n(e.g., while adding the event listener, to get its return value)');
- }
- }
- // this listener will run the foo function, get its return value, *then* attempt to add the listener
- window.addEventListener('click', foo(), false);
- // since foo() doesn't return anything, it actually does this
- window.addEventListener('click', undefined, false);
- // foo() does get executed once, because you invoked it to get its return value
- // but it will not actually add a listener
- // now let's add a real listener, and our foo() function will tell us some info whenever we call it
- window.addEventListener('click', foo, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement