开发者

Make a function accessible outside of a closure

开发者 https://www.devze.com 2023-03-01 09:35 出处:网络
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 b

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

精彩评论

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