Javascript 如何全局公开和触发onready函数?
我将如何公开下面的两个函数以供全局访问<代码>函数WithInReady()代码>和Javascript 如何全局公开和触发onready函数?,javascript,jquery,Javascript,Jquery,我将如何公开下面的两个函数以供全局访问函数WithInReady()和无柄物品() 在主题的范围内我可以触发以下功能: theme.handleSomething(); theme.functionWithinOnReady(); 假设我需要从第三个脚本(/全局)触发函数,我将如何以最好的方式实现这一点 (function($) { "use strict"; var theme = { onReady : function(){ f
无柄物品()代码>
在主题的范围内
我可以触发以下功能:
theme.handleSomething();
theme.functionWithinOnReady();
假设我需要从第三个脚本(/全局)触发函数,我将如何以最好的方式实现这一点
(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);
您可以将主题
附加到窗口(全局)或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);
现在,您可以在任何地方访问$.theme
,如果要在jQuery集合上使用它,您可以使用$.fn.theme
来原型化它。window.theme={
。这会公开主题中的所有函数吗?因为我只想公开主题中的部分函数,所以您无法选择对象的哪些属性被公开,要么是整个对象,要么什么都没有。如果您想要特定的属性,请使用另一个对象引用中所需的属性e
您是否有后一种方法的示例,或者我可以在哪里查阅该方法的一些示例?