Make a function accessible outside of a closure
Is there a way to make function created inside a closure accessible outside of the closure? I'm working with an AIR app and I need to provi开发者_如何学JAVAde access to the specialFunction()
to AIR but the closure is keeping that from happening.
(function () {
... a bunch of code ..
function specialFunction() {
.. some code
}
}());
You can assign the function to the global object (which is window
in browsers):
(function () {
... a bunch of code ..
window.specialFuncton = function() {
.. some code
}
}());
This makes it globally available.
If the AIR application also needs access to other functions, then it is better to create a namespace for these functions:
var funcs = {}; // global
(function () {
... a bunch of code ..
funcs.specialFuncton = function() {
.. some code
}
}());
精彩评论