开发者

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
    }
}());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜