Created
January 10, 2014 12:43
-
-
Save anonymous/8351305 to your computer and use it in GitHub Desktop.
Various methods of calling functions defined out of scope.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add to global namespace | |
$(document).ready(function(){ | |
window.coolfunc = function(arg){ | |
alert(arg); | |
} | |
coolfunc('Hi!'); | |
}); | |
coolfunc('oh hi too!'); | |
// Declare your own name space (http://stackoverflow.com/a/3402906/662826) | |
$(document).ready(function(){ | |
MyApp = { //Example from SO shows var MyApp, but this doesn't make the var global, so for use within .ready() we must omit 'var' | |
showMessage: function(arg){ | |
alert(arg); | |
} | |
}; | |
MyApp.showMessage(); | |
}); | |
MyApp.showMessage(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment