简体   繁体   English

如何全局公开和触发 onready 功能?

[英]How to expose and trigger onready function globally?

How would I go about exposing the following two functions below for global access?我将如何公开以下两个函数以进行全局访问? functionWithinOnReady(); and handleSomething();handleSomething();

Within the scope of theme I can trigger the functions with:theme范围内,我可以通过以下方式触发功能:

theme.handleSomething();
theme.functionWithinOnReady();

Assuming I would need to trigger the functions from a third script (/globally), how would I go about this in the best way?假设我需要从第三个脚本(/全局)触发这些函数,我将如何以最佳方式执行此操作?

(function($) {
    "use strict";

    var theme = {

        onReady : function(){
            functionWithinOnReady = function() {
                //want to access this globally
            }
        },
        handleSomething: function() {
            //want to access this globally
        },
        onLoad : function() { },
        onResize : function() { }
    };


    $(document).ready( theme.onReady );
    $(window).load( theme.onLoad );
    $(window).resize( theme.onResize );

})(jQuery);

You could attach theme to either the window (global) or jQuery您可以将theme附加到窗口(全局)或 jQuery

(function($) {
    "use strict";

    $.theme = {

        onReady : function(){
            functionWithinOnReady = function() {
                //want to access this globally
            }
        },
        handleSomething: function() {
            //want to access this globally
        },
        onLoad : function() { },
        onResize : function() { }
    };


    $(document).ready( $.theme.onReady );
    $(window).load( $.theme.onLoad );
    $(window).resize( $.theme.onResize );

})(jQuery);

Now you can access $.theme anywhere, and if it's supposed to be used on jQuery collections, you could prototype it with $.fn.theme instead.现在你可以在任何地方访问$.theme ,如果它应该用于 jQuery 集合,你可以用$.fn.theme来代替它的原型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM